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.
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;
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;
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
A simple typo 🙂
Okay thank you very much.
I'm trying to select max value(val) per group per name with the condition to keep tie values(val)
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.
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.
Ready to level-up your skills? Choose your own adventure.