To Display Data in Sorted Order – use SORT Procedure. The are some interesting features of this procedure. SORT procedure can be used to –
- Rearrange observations in a data set.
- Create new data set with rearranged observations.
- Sort on multiple variables.
- Sort in ascending or descending order.
While using SORT procedure keep in mind that:
- It sorts in ascending order by default.
- It replaces the original data set with the new data set.
- It replaces missing values with smallest possible values.
In the following example, we will use cars2 data set(refer to previous posts) and sort it on msrp (Manufacturer’s Suggested Retail Price) and mpg_city ( Miles per Gallon in city) to form a new data set cars3.
The PROC SORT step sorts the SAS data set cars2 by the values of the variable msrp within the values of the variable mpg_city and creates the temporary SAS data set cars3. Then the PROC PRINT step prints the cars3 data set.
Suppose we prefer cars that are fuel efficient and relatively low priced at the same time, we can sort mpg_city in descending order. Here we add the DESCENDING option before mpg_city to the BY statement . Notice that the DESCENDING applies only to the variable mpg_city (msrp is sorted in ascending order just like before).