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.

 

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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