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

@Kurt_Bremser 

 

for rows age >= 24 months, sort by AGE and KEY, keep record closest to Month 24. if mutiple KEYS at same time point, keep highest KEY.

 

IDABKeyDAge
1035310882
1031910882
1035321889
10353458817
10319358817
10397378848
10397498860
10397698896
1031051988108
103977988132
103978988156
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Yeah, missed that.

So we sort with a WHERE first:

proc sort
  data=have (where=(age ge 24))
  out=sorted
;
by id age descending key;
run;

data want;
set sorted;
by id;
if first.id;
run;

View solution in original post

11 REPLIES 11
PeterClemmensen
Tourmaline | Level 20

What does your desired result look like here?

Kurt_Bremser
Super User

First, I'd build a value that is

diff = abs(24 - age);

Then, I would sort

by id descending diff descending key;

and, in a final data step, do

by id;
if first.id;
bharath86
Obsidian | Level 7

@PeterClemmensen @Kurt_Bremser 

 

I have picked up a better example, use this data pls. (this applies to records >=24 only )

There are two rows with age 48, however I should keep one record close to 24 (so both rows with age 48 satisfy this condition) but then I have multiple keys (7,9)  at same time point (48), so I need to keep the record with highest key value. 

 

if its same key at different time points like 48 and 96 it doesnt really matter as I am going to keep the closest to 24 which still has the same key as the next. 

IDABKeyDAge
1035310882
1031910882
1035321889
10353458817
10319358817
10397378848
10397498848
10397698896
1031051988108
103977988132
103978988156
bharath86
Obsidian | Level 7
Thats why I clearly stated this is applicable to records >= 24 ONLY. I am not worried about record below 24.
Kurt_Bremser
Super User

Yeah, missed that.

So we sort with a WHERE first:

proc sort
  data=have (where=(age ge 24))
  out=sorted
;
by id age descending key;
run;

data want;
set sorted;
by id;
if first.id;
run;
bharath86
Obsidian | Level 7
I think the output is correct, but what if I also want to keep records below 24 with highest key value along with the above code.
bharath86
Obsidian | Level 7
But that doesnt give the highest key below 24, it gives highest of age. when executed with second answer.
Kurt_Bremser
Super User

Please post your source data in usable form (data step with datalines, do not skip this!), the code you ran, and indicate where the result does not meet your expectations.

bharath86
Obsidian | Level 7

@Kurt_Bremser 

 

Thank you very much. 

 

This logic can actually be applied in either case if records < 24 or > 24 just by tweaking it a bit. 

 

Thank you again. 

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
  • 11 replies
  • 1172 views
  • 0 likes
  • 3 in conversation