BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sai_ch
Obsidian | Level 7

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  
IDRankDiff IDRankDiff
0001220    
000138 000138
0001444    
0001538    
0002218    
000243 000243
000256    
00021230    
0003111 0003111
0003223    
0003347    
00035

2

    
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

 

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;

View solution in original post

2 REPLIES 2
Reeza
Super User

 

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;
sai_ch
Obsidian | Level 7

Thanks Reeza for the solution.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 967 views
  • 2 likes
  • 2 in conversation