Monday, November 28, 2016

Hack 4.3 Computing Smallest and Largest Value for Variables in An Observation

SAS Programing Professionals,

Did you know that you can easily compute the smallest or the largest value for a group of variables within an observation in a DATA step?

The SMALLEST and LARGEST functions return the smallest and largest value, respectively, for the group of values that you specify.  You can also have these functions return the second from smallest (or largest) value, third from smallest (or largest) value, fourth from smallest (or largest) value, and so on.  The form of these functions is:

SMALLEST(k, value-1<,value-2,…>)

LARGEST(k, value-1<,value-2,…>)

…where k = is a numeric constant, variable, or expression that specifies which value to return.  And, value-1 (value-2, etc.) specifies the value of a numeric constant, variable, or expression to be processed.

Here is an example:

options nodate nonumber;

data class(keep=oldest youngest agerange);

array ages{19} ages1 - ages19;

do i = 1 to totobs;

set sashelp.class nobs=totobs;

ages{i} = age;

end;

label youngest = "Age of Youngest Participant"
      oldest   = "Age of Oldest Participant"
      agerange = "Range of Participant Ages"
      ;

youngest = smallest(1,of ages1-ages19);
oldest   = largest(1,of ages1-ages19);
agerange = oldest - youngest;

run;

proc print noobs data=class label;
run;

In this example, we create an array (AGES) to contain the ages for all participants in our study data set.  We use the DO loop to iterate through the CLASS data set and load the ages of the participants into the array.  Then, we use the SMALLEST and the LARGEST functions to return the smallest and largest age values stored in the array.  We also calculate the range of ages.

If we had wanted the next to smallest age value, we would have coded:

 youngest = smallest(2,of ages1-ages19);

Get the picture?

If you rolled your eyes when reviewing this contrived example, think how powerful these functions are when you have a flattened patient file of admission/discharge dates.  You can quickly and easily compute the earliest or latest admission dates as well as the earliest or latest discharge dates.  These functions are in the top compartment of my own SAS tips-and-techniques toolbox!

Best of luck in all your SAS endeavors!

----MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)

Excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers
http://www.amazon.com/Michael-A.-Raithel/e/B001K8GG90/ref=ntt_dp_epwbk_0