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

Hi everyone, I need help with this data.

 

data m_data;
input group name $ val;
datalines;
1 pet 10
1 pet 15
2 jane 20
2 john 21
3 james 30
3 james 30
;

proc sort data=m_data;
by group name descending val;
run;

data tied_values;
set m_data;
by group name descending val;

if first.name;
run;

proc print data=tied_values;
run;

 

I'm trying to keep the first.name, in addition I want to keep values that are tied in the by group such that my final dataset will give this;

1 pet 15
2 jane 20
2 john 21
3 james 30
3 james 30

Using the first.name alone will produce this;

1 pet 15
2 jane 20
2 john 21
3 james 30

Please what concept can I use to get this done.

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

I think this is what you want

 

data m_data;
input group name $ val;
datalines;
1 pet 10
1 pet 15
2 jane 20
2 john 21
3 james 30
3 james 30
;

proc sql;
   create table want as
   select *
   from m_data
   group by group, name
   having max(val) = val
   order by group
   ;
quit;

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

I think this is what you want

 

data m_data;
input group name $ val;
datalines;
1 pet 10
1 pet 15
2 jane 20
2 john 21
3 james 30
3 james 30
;

proc sql;
   create table want as
   select *
   from m_data
   group by group, name
   having max(val) = val
   order by group
   ;
quit;
PrinceAde
Obsidian | Level 7

This works. If I may ask, what's the logic behind listing the group variable twice in the order by statement?

Can this same code/logic be implemented in a data step as well?

 

Thank you @JosvanderVelden 

PeterClemmensen
Tourmaline | Level 20

A simple typo 🙂

PrinceAde
Obsidian | Level 7

Okay thank you very much.

JosvanderVelden
SAS Super FREQ
The row/observation "3 james 30" is present twice in the input/source dataset so sort with first logic will not allow a result as you wish.
What is the logic behind keeping these 2 exactly the same observations/rows?
PrinceAde
Obsidian | Level 7

I'm trying to select max value(val) per group per name with the condition to keep tie values(val)

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!

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.

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