Hi I have the following
id1 id2
01 1
01 1
01 2
02 1
02 1
02 1
How can I ideintify the different value id2 within id1?
For that result:
proc sort data=have;
by id1 id2;
run;
data want;
set have;
by id1 id2;
if first.id1 then flag=1;
else if first.id2 then flag + 1;
run;
This both identifies and counts for you:
proc freq data=have;
tables id1 * id2 / list missing;
run;
@SASSLICK001 wrote:
I wanted to use first. and last.
It would help to show what you expect for output.
Maybe something like ?
data want; set have; by id1 id2; if first.id2; run;
I want to create a flag where the value changes per change in value for id2
expected output
id1 id2 flag
01 x 1
01 x 1
01 y 2
02 z 1
02 z 1
02 z 1
Assuming your data is sorted by ID1 and ID2 as implied by your example:
data have; input id1 $ id2 $; datalines; 01 x 01 x 01 y 02 z 02 z 02 z ; run; data want; set have; by id1 id2; if first.id1 then flag=0; else flag = first.id2; run;
I prefer the 0/1 coding as 0 means 'not a change' in this case and there are some advantages that I can count the number of changes in id1 by getting a sum of the flag variable.
If your data is not sorted then you need to provide a more realistic example of the data.
For that result:
proc sort data=have;
by id1 id2;
run;
data want;
set have;
by id1 id2;
if first.id1 then flag=1;
else if first.id2 then flag + 1;
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.