DATA Step, Macro, Functions and more

Getting the character value that corresponds to the minimum numeric variable

Reply
Contributor jei
Contributor
Posts: 74

Getting the character value that corresponds to the minimum numeric variable

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!

PROC Star
Posts: 1,760

Re: Getting the character value that corresponds to the minimum numeric variable

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;

 

PROC Star
Posts: 768

Re: Getting the character value that corresponds to the minimum numeric variable

Tons of ways to do this, here is a datastep solution Smiley Happy

 

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;
Super User
Posts: 11,343

Re: Getting the character value that corresponds to the minimum numeric variable

[ Edited ]

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.

 

 

Ask a Question
Discussion stats
  • 3 replies
  • 103 views
  • 0 likes
  • 4 in conversation