0

This is the follow up of another post I made before.

Turns out that, when creating that "counter" variable I want to include a condition so if a variable called "outcome" takes value "out of time", the counter is 0, but it shouldnt reset the count for that Id on that date.

This is my code as it is now:

DATA want;
SET have;
BY  ID date time;
RETAIN attempt;

IF first.Date then RealAttempts = 1;
ELSE RealAttempts = min(3,RealAttempts+1);
IF Outcome ="Out Of Time" then RealAttempts=0;
run;

And this is how it should look:

data have;
infile datalines dsd delimiter=',';
input date :ddmmyy10. ID  time :time8.  Outcome :$40. Attempt  ;
format date ddmmyy10.;
format time time8.;
datalines;
05/11/2020,1000,8:15:23,"Answered",1,
05/11/2020,1000,8:20:10,"Out Of Time",0
05/11/2020,1000,8:21:10,"Answered",2
05/11/2020,1000,9:05:15,"Out Of Time",0
;

But this is how it looks (on 3rd row it resets to 1 instead of 2)

data have;
infile datalines dsd delimiter=',';
input date :ddmmyy10. ID  time :time8.  Outcome :$40. Attempt  ;
format date ddmmyy10.;
format time time8.;
datalines;
05/11/2020,1000,8:15:23,"Answered",1,
05/11/2020,1000,8:20:10,"Out Of Time",0
05/11/2020,1000,8:21:10,"Answered",1
05/11/2020,1000,9:05:15,"Out Of Time",0
;

Any idea?

amestrian
  • 546
  • 3
  • 12
  • This seems to be your problem then : `IF Outcome ="Out Of Time" then RealAttempts=0;` but I suspect that will introduce another problem? – Reeza Feb 23 '21 at 20:17
  • @reeza precisely! I believe that is the problem as well, except I haven't been able to put it in the correct way (because I still need it obviously) – amestrian Feb 23 '21 at 20:54
  • "I still need it obviously" -> no, you don't. Make sure to check your assumptions there, you can introduce a new variable or different variable to accomplish your requirements. – Reeza Feb 23 '21 at 21:04
  • @Reeza I do need the "outcome" condition... that's what I meant – amestrian Feb 23 '21 at 21:48

1 Answers1

1

If I understand what you want just add a second variable that has the values you want to actually report and leave the "counter" alone. So you set this reporting field to zero on your condition and otherwise have it return the truncated count.

DATA want;
  SET have;
  BY  ID date time;
  RETAIN RealAttempts ;

  IF first.Date then RealAttempts = 1;
  ELSE RealAttempts = RealAttempts+1;

  IF Outcome ="Out Of Time" then ReportedAttempts=0;
  ELSE ReportedAttempts=min(3,RealAttempts);
run;

If "Out of Time" observations should also not increment the counter then change the logic even more.

DATA want;
  SET have;
  BY  ID date time;
  RETAIN RealAttempts ;

  IF first.Date then RealAttempts = 0;

  IF Outcome ="Out Of Time" then ReportedAttempts=0;
  ELSE do ;
    RealAttempts = RealAttempts+1;
    ReportedAttempts=min(3,RealAttempts);
  end;
run;
Tom
  • 47,574
  • 2
  • 16
  • 29
  • That makes sense but it doesn't give the expected result. On the third line the attempt should be "2" but it's returning a "3" – amestrian Feb 24 '21 at 10:53
  • Sounds like you want to also skip the increment when "Out of Time"? – Tom Feb 24 '21 at 19:36