Hello Everyone,
The Have dataset is sorted by ID and Ranks. For each group, by ID, I am trying to pick the first observation for which the difference is less than equal to 12. For example, for ID 0001, the Want dataset has the secon observation. Would really appreciate if anyone could help me out.
Thanks
Have | Want | |||||
ID | Rank | Diff | ID | Rank | Diff | |
0001 | 2 | 20 | ||||
0001 | 3 | 8 | 0001 | 3 | 8 | |
0001 | 4 | 44 | ||||
0001 | 5 | 38 | ||||
0002 | 2 | 18 | ||||
0002 | 4 | 3 | 0002 | 4 | 3 | |
0002 | 5 | 6 | ||||
0002 | 12 | 30 | ||||
0003 | 1 | 11 | 0003 | 1 | 11 | |
0003 | 2 | 23 | ||||
0003 | 3 | 47 | ||||
0003 | 5 | 2 |
Create a flag that indicates if you've found the record. So your logic becomes less than or equal to 12 AND flag is not set.
data want;
set have;
by id;
retain flag;
if first.id then flag=0;
if diff<=12 and flag=0 then do;
output;
flag=1;
end;
run;
Create a flag that indicates if you've found the record. So your logic becomes less than or equal to 12 AND flag is not set.
data want;
set have;
by id;
retain flag;
if first.id then flag=0;
if diff<=12 and flag=0 then do;
output;
flag=1;
end;
run;
Thanks Reeza for the solution.
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.
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.