6.4 - Sorting Data

By default, the PRINT procedure displays observations in the order in which they appear in your data set. Alternatively, you can use the SORT procedure to first sort your data set based on the values of one or more variables. Then, when you use the PRINT procedure, SAS will display the observations in the order in which you sorted the data.

Example 6.9 Section

The following SAS program uses the SORT procedure to sort the basic data set first by clinic name (clinic) and then by the number of visits (no_vis) before printing the (sorted) data set srtd_basic:

PROC SORT data = basic out = srtd_basic;
   by clinic no_vis;
RUN;

PROC PRINT data = srtd_basic NOOBS;
   var clinic no_vis subj name gender type_vis expense;
RUN;
clinic no_vis subj name gender type_vis expense
ALTO 1 1201 Benedict Arnold 2 190 1450.23
ALTO 10 1168 Thomas Jones 2 190 3904.89
LEWN 2 1167 Maryann White 1 101 2999.34
LEWN 7 1024 Alice Smith 1 101 1001.98
MNMC 5 1980 Jane Smiley 1 190 3567.00
MNMC 6 1471 John Smith 2 187 1763.09
MNMC 7 1302 Felicia Ho 1 190 1209.94

First, launch and run the SAS program, and review the output to convince yourself that the output from the srtd_basic data set is in order first by clinic and then by no_vis.

Then, note that while the SORT procedure's BY statement is required, it's OUT= option is optional. If you don't use it, however, then the SORT procedure permanently sorts the data set that is specified in the DATA= option. Therefore, if you need your data to be sorted just to produce output temporarily, then you should use the OUT= option in conjunction with a temporary SAS data set name.

Oh, I guess you should also note that, by default, SAS sorts the values of the variables appearing in the BY statement in ascending order. If you want them sorted in descending order, you need to use the BY statement's DESCENDING option.

Example 6.10 Section

The following SAS program uses the BY statement's DESCENDING option to tell SAS to sort the basic data set first by clinic name (clinic) in descending order, and then by the number of visits (no_vis) in ascending order:

PROC SORT data = basic out = srtd_basic;
   by descending clinic no_vis;
RUN;

PROC PRINT data = srtd_basic NOOBS;
   var clinic no_vis subj name gender type_vis expense;
RUN;
clinic no_vis subj name gender type_vis expense
MNMC 5 1980 Jane Smiley 1 190 3567.00
MNMC 6 1471 John Smith 2 187 1763.09
MNMC 7 1302 Felicia Ho 1 190 1209.94
LEWN 2 1167 Maryann White 1 101 2999.34
LEWN 7 1024 Alice Smith 1 101 1001.98
ALTO 1 1201 Benedict Arnold 2 190 1450.23
ALTO 10 1168 Thomas Jones 2 190 3904.89

First, launch and run the SAS program, and review the output to convince yourself that the output from the srtd_basic data set is in descending order of clinic and in ascending order of no_vis. That is, if your BY statement contains more than one variable, then the DESCENDING option applies only to the variable that immediately follows it. You might want to sandwich another DESCENDING between clinic and no_vis in the BY statement and then re-run the SAS program to see the effect.