SORT <DATA=SAS-data-set> <OUT=SAS-data-set> BY <DESCENDING variables> ;
The SORT statement sorts a SAS data set. You can use the following clauses with the SORT statement:
names the SAS data set to be sorted. It can be specified with a one-level name (for example, A) or a two-level name (for example, Sasuser.A). You can also specify an expression (enclosed in parentheses) that resolves to the name of a SAS data set. (See the example for the CLOSE statement .) Note that the DATA= portion of the specification is optional.
specifies a name for the output data set. If this clause is omitted, the DATA= data set is replaced by the sorted version.
specifies the variables to be sorted. A BY clause must be used with the SORT statement.
specifies the variables are to be sorted in descending order.
The SORT statement sorts the observations in a SAS data set by one or more variables, stores the resulting sorted observations in a new SAS data set, or replaces the original.
In contrast with other data processing statements, it is mandatory that the data set to be sorted be closed prior to the execution of the SORT statement. The SORT statement gives an error if you try to sort a data set that is open.
The SORT statement first arranges the observations in the order of the first variable in the BY clause; then it sorts the observations with a given value of the first variable by the second variable, and so forth. Every variable in the BY clause can be preceded by the keyword DESCENDING to denote that the variable that follows is to be sorted in descending order. Note that the SORT statement retains the same relative positions of the observations with identical BY variable values.
For example, the following statement sorts data from the Sashelp.Class
data set by the variables Age
and Height
, where Age
is sorted in descending order, and all observations with the same Age
value are sorted by Height
in ascending order:
sort Sashelp.Class out=sortClass by descending age height;
The output data set sortClass
contains the sorted observations. When a data set is sorted in place (without the OUT= clause) any indexes associated with
the data set become invalid and are automatically deleted.
Notice that all the clauses of the SORT statement must be specified in the order given in the syntax.