BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
shubham_d
Fluorite | Level 6

HAVE - 

 

IDNAMESCORECODE
1John21ABC
1John22XYZ
1John23ABC
2Mark4XYZ
2Mark6XYZ
2Mark5ABC

 

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. 

 

IDNAMESCORECODE
1John22XYZ
1John23ABC
2Mark6XYZ
2Mark5ABC
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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;
--
Paige Miller

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

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;
--
Paige Miller
shubham_d
Fluorite | Level 6
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
PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
shubham_d
Fluorite | Level 6
Hey! My bad, will keep this in mind as I interact more in this community. By the way, the code worked fine! Not sure why I did not receive the results in the first run. Thanks a lot!!
shubham_d
Fluorite | Level 6

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;


IDNAMESCORECODE
1John23A05
1John26A02
1John26A01
2Mark21A05
2Mark22A05
2Mark23A07

 

WANT - 

 

IDNAMESCORECODE
1John26A02 [ Same Order as Input ]
1John26A01
1John23A05
2Mark22A05
2Mark23A07

 

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. 

ballardw
Super User

Are there always exactly three records per Id?

What do you want if there is only one record? Two?

shubham_d
Fluorite | Level 6
Hey! No, the records will vary for each Id. If the records are unique with respect to code, I want to keep them as it is.
jebjur
SAS Employee

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;

SAS Innovate 2025: Register Now

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!

SAS Enterprise Guide vs. SAS Studio

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 8 replies
  • 1002 views
  • 0 likes
  • 4 in conversation