Hi all,
My dataset:
ID1 ID2
1 a
1 a
1 b
2 a
2 a
2 b
2 b
Dataset I want:
ID1 ID2 varwant
1 a 1
1 a .
1 b 2
2 a 1
2 a .
2 b 2
2 b .
How do I do this?
Thx!
Here's a possibility that assumes your data set is in the right order:
data want;
set have;
by id1 id2;
if first.id1 then counter=1;
else if first.id2 then counter + 1;
if first.id2 then varwant = counter;
drop counter;
run;
Also, before dropping COUNTER, take a look at it. Perhaps you prefer that to the VARWANT in your post.
By definition, on the first observation for an ID1, both first.id1 and first.id2 are true. Without "else" therefore, the first COUNTER for an ID1 would get incremented twice, ending up with a value of 2 instead of 1. "Else" prevents incrementing COUNTER that second time, until the next time that a new ID2 appears.
data have;
input ID1 ID2 $;
cards;
1 a
1 a
1 b
2 a
2 a
2 b
2 b
;
data want;
set have;
by id1 id2 notsorted;
if first.id1 then n=0;
n+first.id2;
want=n;
if not first.id2 then call missing(want);
drop n;
run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.