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

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 6 replies
  • 1448 views
  • 0 likes
  • 3 in conversation