Hi, I have a question about assigning same sequence numbers within each group of 2 variables. Thanks much for your help! My data looks like below.
ID | count |
1 | 15 |
1 | 13 |
1 | 13 |
1 | 8 |
2 | 20 |
2 | 20 |
2 | 20 |
2 | 14 |
2 | 14 |
2 | 6 |
2 | 6 |
What I want is to create seq_ID as below.
ID | count | seq_id |
1 | 15 | 1 |
1 | 13 | 2 |
1 | 13 | 2 |
1 | 8 | 3 |
2 | 20 | 1 |
2 | 20 | 1 |
2 | 20 | 1 |
2 | 14 | 2 |
2 | 14 | 2 |
2 | 6 | 3 |
2 | 6 | 3 |
Very close. Try:
data want;
set have;
by id count nosorted;
if first.id then seq_id=1;
else if first.count then seq_id+1;
run;
Thanks, but this is not working.
1, The count variable was sorted descending
2, I want to assign SAME seq number within each group of same id and same count.
Very close. Try:
data want;
set have;
by id count nosorted;
if first.id then seq_id=1;
else if first.count then seq_id+1;
run;
Thanks Astounding, but it does not work at all. The error message shows
BY variables are not properly sorted on data set. I think that because count variable was sorted descending. I need keep the biggest number of count by the end.
Good catch. I might even go a step further:
by id count notsorted;
We really don't have enough insight into the pattern of COUNT and whether it can go both up and down.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.