Sorting Data in SAS

To Display Data in Sorted Order – use SORT Procedure. The are some interesting features of this procedure. SORT procedure can be used to –

  1. Rearrange observations in a data set.
  2. Create new data set with rearranged observations.
  3. Sort on multiple variables.
  4. Sort in ascending or descending order.

While using SORT procedure keep in mind that:

  1. It sorts in ascending order by default.
  2. It replaces the original data set with the new data set.
  3. 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.

program_sas

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

program_sas

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s