DATA Step, Macro, Functions and more

Picking an observation from a group

Accepted Solution Solved
Reply
Contributor
Posts: 34
Accepted Solution

Picking an observation from a group

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

    

Accepted Solutions
Solution
‎07-29-2016 05:35 PM
Super User
Posts: 19,878

Re: Picking an observation from a group

 

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


All Replies
Solution
‎07-29-2016 05:35 PM
Super User
Posts: 19,878

Re: Picking an observation from a group

 

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;
Contributor
Posts: 34

Re: Picking an observation from a group

Thanks Reeza for the solution.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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