BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I have the following variables (id and score) and would like to know how to get the third variable maxscore:
id score maxscore
1 5 7
1 7 7
1 3 7
2 2 8
2 1 8
2 8 8

What is the code that I'll need to use to get maxscore to pick the largest value from the observations for each id?
Thank you for your help!
2 REPLIES 2
deleted_user
Not applicable
> I have the following variables (id and score) and
> would like to know how to get the third variable
> maxscore:
> id score maxscore
> 1 5 7
> 1 7 7
> 1 3 7
> 2 2 8
> 2 1 8
> 2 8 8
>
> What is the code that I'll need to use to get
> maxscore to pick the largest value from the
> observations for each id?
> Thank you for your help!


You can use either a Proc SQL step or a data step to find a solution. SQL may appear concise but it also preprocesses the data to arrive at the max score for each ID. I have presented only the SQL solution below:

data NOmaxscore ;
input id score ;
cards ;
1 5
1 7
1 3
2 2
2 1
2 8
run ;

proc sql ;
create table WITHmaxscore as
select * , max(score) as maxscore
from NOmaxscore
group by id ;
quit ;

options nocenter ;
proc print ;
run ;

And this yields:

Obs id score maxscore

1 1 5 7
2 1 3 7
3 1 7 7
4 2 8 8
5 2 1 8
6 2 2 8

Venky Chakravarthy
deleted_user
Not applicable
Thank you very much!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

New Learning Events in April

 

Join us for two new fee-based courses: Administrative Healthcare Data and SAS via Live Web Monday-Thursday, April 24-27 from 1:00 to 4:30 PM ET each day. And Administrative Healthcare Data and SAS: Hands-On Programming Workshop via Live Web on Friday, April 28 from 9:00 AM to 5:00 PM ET.

LEARN MORE

Discussion stats
  • 2 replies
  • 2797 views
  • 0 likes
  • 1 in conversation