HAVE -
ID | NAME | SCORE | CODE |
1 | John | 21 | ABC |
1 | John | 22 | XYZ |
1 | John | 23 | ABC |
2 | Mark | 4 | XYZ |
2 | Mark | 6 | XYZ |
2 | Mark | 5 | ABC |
WANT - If there is an ID with the same code, I want to keep the row that has a higher score & drop the one with a lower score. PS - We have to deduce this ID-wise. It's okay to have the same CODE across different IDs.
ID | NAME | SCORE | CODE |
1 | John | 22 | XYZ |
1 | John | 23 | ABC |
2 | Mark | 6 | XYZ |
2 | Mark | 5 | ABC |
UNTESTED CODE (please provide your data as SAS data step code if you want tested code)
proc sort data=have;
by id code score;
run;
data want;
set have;
by id code;
if last.code;
run;
UNTESTED CODE (please provide your data as SAS data step code if you want tested code)
proc sort data=have;
by id code score;
run;
data want;
set have;
by id code;
if last.code;
run;
@shubham_d wrote:
Data -
data have;
input id name $ score code $;
datalines;
1 John 21 ABC
1 John 22 XYZ
1 John 21 ABC
2 Mark 4 XYZ
2 Mark 6 XYZ
2 Mark 5 XYZ
;
run;
Tried running your code & got an empty data set
Works for me. It is never helpful to say something didn't work, and not provide details. In this case, we need to see the complete log.
Hi Paige,
If the score is the same, I would like to maintain the order of "Code" as it is in Have.
Facing an issue in a scenario where my data is -
HAVE -
data have;
input id name $ score code $;
datalines;
1 John 23 A05
1 John 26 A02
1 John 26 A01
2 Mark 21 A05
2 Mark 22 A05
2 Mark 23 A07
;
run;
ID | NAME | SCORE | CODE |
1 | John | 23 | A05 |
1 | John | 26 | A02 |
1 | John | 26 | A01 |
2 | Mark | 21 | A05 |
2 | Mark | 22 | A05 |
2 | Mark | 23 | A07 |
WANT -
ID | NAME | SCORE | CODE |
1 | John | 26 | A02 [ Same Order as Input ] |
1 | John | 26 | A01 |
1 | John | 23 | A05 |
2 | Mark | 22 | A05 |
2 | Mark | 23 | A07 |
Using the solution you provided, the A01 is getting sorted & coming at the top, whereas I want to keep the order as it is when the score is equal.
Are there always exactly three records per Id?
What do you want if there is only one record? Two?
This code works for the sample data you provided:
data have;
input id name $ score code $;
datalines;
1 John 21 ABC
1 John 22 XYZ
1 John 23 ABC
2 Mark 4 XYZ
2 Mark 6 XYZ
2 Mark 5 ABC
;
run;
proc sort data=have;
by id code score;
run;
data want;
set have;
by id code score;
if last.code;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.