Hello,
I am trying to create an indicator variable to filter some observations out and count how many observations I need. Basically, what I have is this:
data have=
ID answer
1 99
1 22
1 53
1 99
1 99
1 99
2 34
2 57
2 99
2 99
2 99
2 99
What I want is for times when 99 is the last recorded answer to go back as far as the 99s go back to have a new variable mark it as a 1.
ID answer indicator
1 99 0
1 22 0
1 53 0
1 99 1
1 99 1
1 99 1
2 34 0
2 57 0
2 99 1
2 99 1
2 99 1
2 99 1
I know how to get a 1 for all of the 99s, but the part where it's only the last one going back I don't know how to get. Any help would be great.
Thanks.
I am not going to give working code as I am not typing that test data in - provide test data in the form of a datastep if you want working code. The theory is quite simple however, proc sort the data so the data is in reverse order, then retain the indicator variable, then an if statement = 99 then set indicator to 1, and when not 99 set to zero again.
@RW9 wouldn't that mean that the first occurence of answer being 99 will be given an indicator variable value of 1 when it is supposed to be zero?
Have a secondary flag to show if the event has been triggered already and drop it.
Ah yes of course, makes sense.
data have;
input ID answer;
cards;
1 99
1 22
1 53
1 99
1 99
1 99
2 34
2 57
2 99
2 99
2 99
2 99
;
run;
data tmp;
set have;
n+1;
run;
proc sort data=tmp; by descending n id;run;
data tmp;
set tmp;
length indicator 8;
by descending id descending n;*assumes that id in have is provided in ascending sequence;
retain indicator;
if first.id and answer eq 99 then do;
indicator=1;
end;
if answer ne 99 then indicator=0;
run;
proc sort data=tmp out=tmp(drop=n); by n; run;
Cheers
- Cheers -
So you only want indicator=1 when there are multiple 99s ? data have; input ID answer; cards; 1 99 1 22 1 53 1 99 1 99 1 99 2 34 2 57 2 99 2 99 2 99 2 99 ; run; data want; n=0;flag=0; do until(last.answer); set have; by id answer notsorted; n+1; end; do until(last.answer); set have; by id answer notsorted; if answer=99 and n gt 1 then flag=1; output; end; drop n; run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.