BookmarkSubscribeRSS Feed
jei
Quartz | Level 8 jei
Quartz | Level 8

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!

3 REPLIES 3
ChrisNZ
Tourmaline | Level 20

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;

 

PeterClemmensen
Tourmaline | Level 20

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;
ballardw
Super User

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.

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 781 views
  • 0 likes
  • 4 in conversation