Hi,
I have the following dataset
IDs VAR1
12345 1
12345 2
12345 3
12345 4
12345 5
12345 6
63826 2
63826 3
63826 4
63826 5
63826 6
90283 32
90283 33
90283 34
90283 35
I just want to get the IDs with minimum VAR values. The variable IDs and VAR are numeric variables.
This is the example of what I should get:
IDs VAR1
12345 1
63826 2
90283 32
Thank you!
There are many ways to do this.
For example:
proc sql;
create table WANT as
select ID, min(VAR1)
from HAVE
group by ID;
quit;
Tons of ways to do this, here is a datastep solution 🙂
data have;
input IDs$ VAR1;
datalines;
12345 1
12345 2
12345 3
12345 4
12345 5
12345 6
63826 2
63826 3
63826 4
63826 5
63826 6
90283 32
90283 33
90283 34
90283 35
;
proc sort data=have;
by IDs VAR1;
run;
data want;
set have;
by IDs VAR1;
if first.IDs;
run;
And another approach:
proc summary data=have nway; class id; var var1; output out=want (drop= _:) min=; run;
I like Proc Summary especially if I have a largish list of variables and/or want different statistics as there is an option / autoname to create uniquely named output varaibles based on the varaible and requested statistic(s) so I don't have to explicitly name many variables.
For example:
proc summary data=have nway; class id; var var1 - var20; output out=want (drop= _:) min= max= std= median= / autoname; run;
would create 80 variables with names like var1_min var1_max var1_stddev var1_median var2_min var2_max etc.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.