Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find max value of variable for each unique observation in "stacked" dataset

Tags:

sas

Sorry for the vauge title.

My data set looks essentially like this:

ID   X
18   1
18   1
18   2
18   1
18   2
369  2
369  3
369  3
361  1

what I want is to find the max value of x for each ID. In this dataset, that would be 2 for ID=18 and 3 for ID=361.

Any feedback would be greatly appreciated.

like image 539
joey Avatar asked Jul 14 '10 18:07

joey


1 Answers

Proc Means with a class statement (so you don't have to sort) and requesting the max statistic is probably the most straightforward approach (untested):

data sample; 
    input id x; 
datalines; 
18  1 
18  1 
18  2 
18  1 
18  2 
369 2 
369 3 
369 3 
361 1 
; 
run; 


proc means data=sample noprint max nway missing; 
   class id;
   var x;
   output out=sample_max (drop=_type_ _freq_) max=;
run; 

Check out the online SAS documentation for more details on Proc Means (http://support.sas.com/onlinedoc/913/docMainpage.jsp).

like image 138
Matthew Nizol Avatar answered Sep 21 '22 15:09

Matthew Nizol