Selecting max value from multiple observations

Reply
Not applicable
Posts: 0

Selecting max value from multiple observations

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!
Not applicable
Posts: 0

Re: Selecting max value from multiple observations

Posted in reply to deleted_user
> 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
Not applicable
Posts: 0

Re: Selecting max value from multiple observations

Posted in reply to deleted_user
Thank you very much!
Ask a Question
Discussion stats
  • 2 replies
  • 1977 views
  • 0 likes
  • 1 in conversation