**Sorting the Data Set**

Sorting the data set is one of the most common data manipulation tasks in SAS.

It can be done by using a procedure called Proc Sort.

Example

Copy and run the **DRINKS **data set from the yellow box below.

The **DRINKS** data set contains a list of hot and cold drinks from Starbucks.

It also has the information about the calories and fat associated with each drink.

The data set is currently sorted by a variable called **TYPE** (in the first column).

The cold drinks are listed followed by the hot drinks.

You can easily change the order of the data set by using Proc Sort.

Example

```
Proc Sort Data=Drinks;
By
```**Name**;
Run;

The procedure Proc Sort has a BY statement on the NAME variable.

This tells SAS to sort the data set by the **NAME **variable.

(try it!)

Let's take a look at another example.

Example

```
Proc Sort Data=Drinks;
By Calories;
Run;
```

With a **BY** statement on the **CALORIES** variable, the data set is sorted by the CALORIES in ascending order.

**Sorting in Descending Order**

By default, the procedure sorts the data set in ascending order.

If descending order is preferred, you can simply add the option **DESCENDING **before the variable in the BY statement.

Example

```
Proc Sort Data=Drinks;
By Descending Calories;
Run;
```

The data set is now sorted by calories in descending order!

Sorting the data set is often required prior to statistical analysis that involves multiple segments.

This will be explained in lesson 6.

**Exercise**

Sort the **DRINKS** data set by FAT in descending order within each drinks type (i.e. Cold and Hot).

You need to have two (2) variables in the BY statement.

Proc Sort Data=Drinks;

By Type Descending Fat;

Run;

Login

Accessing this course requires a login. Please enter your credentials below!