Sorting a SAS Data Set

The observations in a SAS data set can be ordered (sorted) by specific key variables. To sort a SAS data set, close the data set if it is currently open, and issue a SORT statement for the variables by which you want the observations to be ordered. Specify an output data set name if you want to keep the original data set. For example, the following statement creates a new SAS data set named SORTED:

   > sort class out=sorted by name;

The new data set has the observations from the data set CLASS, ordered by the variable NAME.

The following statement sorts in place the data set CLASS by the variable NAME:

   > sort class by name;

However, when the SORT statement is finished executing, the original data set is replaced by the sorted data set.

You can specify as many key variables as needed, and, optionally, each variable can be preceded by the keyword DESCENDING, which denotes that the variable that follows is to be sorted in descending order.