Sentry Page Protection
Proc Sort [1-2]
SAS Base Exam
SAS Base Exam
Proc Sort
The SORT procedure sorts the order of the observations in a data set.
Example
Copy and run the SCHOOL data set from the yellow box below.
Example
Copy and run the SCHOOL data set from the yellow box below.
The SCHOOL data set contains the 3 variables below:
- Name
- Subject
- Results
Let's take a look at an example where we sort the data set by Subject and Results.
Example
Proc Sort Data=School;
By Subject Results;
Run;
The SORT procedure includes a BY statement that specifies how the data set should be sorted.
In our example, the variable Subject and Results are specified in the BY statement.
The data set is now sorted by Subject and Results in ascending order.
Descending Order
You can also sort the data set in descending order.
Simply add the keyword DESCENDING immediately before the variable to be sorted:
The data set will then be sorted by Subject in descending order.
Important Note
The DESCENDING keyword applies only to the variable immediately after the keyword.
In our example, only the Subject variable is sorted in descending order.
The Results variable is still sorted in ascending order.
Exercise
Sort the SCHOOL data set by the students' name in ascending order and their results in descending order.
Need some help?
Fill out my online form.