Previous Page | Next Page

Language Reference

SORT Statement

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:

DATA=SAS-data-set

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). For more information about specifying SAS data sets, see the chapter on SAS data sets in SAS Language Reference: Concepts. Note that the DATA= portion of the specification is optional.

OUT=SAS-data-set

specifies a name for the output data set. If this clause is omitted, the DATA= data set is sorted and the sorted version replaces the original data set.

BY variables

specifies the variables to be sorted. A BY clause must be used with the SORT statement.

DESCENDING

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 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 always retains the same relative positions of the observations with identical BY variable values.

For example, the following statement sorts the SAS data set CLASS 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 class out=sclass by descending age height;

The output data set SCLASS 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.

Note that all the clauses of the SORT statement must be specified in the order given in the preceding list.

Previous Page | Next Page | Top of Page