## SORT Statement

**sorts a SAS data set**
**SORT** * <***DATA=**>SAS-data-set
<**OUT=**SAS-data-set>

** ** **BY**
<**DESCENDING**> variables**;**

where 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-word name (for example,
A) or a two-word name (for example, SASUSER.A).
For more information on 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.
As opposed to all other IML 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 in IML always
retains the same relative positions of the
observations with identical BY variable values.

For example, the IML statement

sort class out=sclass by descending age height;

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.
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 above.

Copyright © 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.