BookmarkSubscribeRSS Feed
jacob_klimek
Obsidian | Level 7

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.

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.  

PeterClemmensen
Tourmaline | Level 20

@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?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Have a secondary flag to show if the event has been triggered already and drop it.

 

PeterClemmensen
Tourmaline | Level 20

Ah yes of course, makes sense.

Oligolas
Barite | Level 11
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 -

Ksharp
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1016 views
  • 0 likes
  • 5 in conversation