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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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