SORT Procedure
KEY Statement
Specifies sorting keys and variables. The KEY statement
is an alternative to the BY statement. The KEY statement syntax allows
for the future possibility of specifying different collation options
for each KEY variable. Currently, the only options allowed are ASCENDING
and DESCENDING.
Restriction: |
The BY statement cannot be used with the KEY statement. |
Tip: |
Multiple KEY statements can be specified. |
Syntax
Required Argument
- variable(s)
-
specifies the variable
by which PROC SORT orders the observations. Multiple variables can
be specified. Each of these variables must be separated by a space.
A range of variables can also be specified. For example, the following
code shows how to specify multiple variables and a range of variables:
data sortKeys;
input x1 x2 x3 x4 ;
cards;
7 8 9 8
0 0 0 0
1 2 3 4 ;
run;
proc sort data=sortKeys out=sortedOutput;
key x1 x2-x4;
run;
Multiple KEY statements
can also be specified. The first sort key encountered from among all
sort keys is considered the primary sort key. Sorting continues for
every specified KEY statement and its variables. For example, the
following code shows how to specify multiple KEY statements:
proc sort data=sortKeys out=sortedOutput;
key x2;
key x3;
run;
The following code
example uses the BY statement to accomplish the same type of sort
as the previous example:
proc sort data=sortKeys out=sortedOutput;
by x2 x3;
run;
Optional Arguments
- ASCENDING
-
sorts in ascending
order the variable or variables that it follows. Observations are
sorted from the smallest value to the largest value. The ASCENDING
keyword modifies all the variables that precede it in the KEY statement.
Alias:ASC
Default:ASCENDING is the default sort order.
Tip:In a PROC SORT KEY statement, the ASCENDING option modifies
all the variables that it follows. The option must follow the
/
.
In the following example, the x1 variable in the input data set is
sorted in ascending order.
proc sort data=sortVar out=sortedOutput;
key x1 / ascending;
run;
- DESCENDING
-
reverses the sort order
for the variable that it follows in the statement so that observations
are sorted from the largest value to the smallest value. The DESCENDING
keyword modifies all the variables that it precedes in the KEY statement.
Alias:DESC
Default:ASCENDING (ASC) is the default sort order.
Tip:In a PROC SORT KEY statement, the DESCENDING option modifies
the variables that follows it. The option must follow the
/
.
In the following example, the x1 and x2 variables in the input data
set is sorted in descending order:
proc sort data=sortVar out=sortedOutput;
key x1 x2 / descending;
run;
The following example uses the BY statement to
accomplish the same type of sort as the previous example:
proc sort data=sortVar out=sortedOutput;
by descending x1 descending x2 ;
run;
Copyright © SAS Institute Inc. All rights reserved.