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.
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.
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.