Javatpoint Logo
Javatpoint Logo

Sorting in SAS Data Sets

Ascending | Descending | BY Statements

In the last topic, we learned the SAS Dataset and saw that Dataset is a vital component and building base of every SAS program. In this topic we will learn about PROC Sort in SAS Data Sets; at the same time, we will learn SAS Default Sorting and Reverse Sorting in SAS. Default Sorting and Reverse Sorting are the two ways of sorting of a dataset. We will also study the BY statement that is essential in sorting of data values.

Sorting in SAS

Sorting in SAS is a process of arrangement of data values (or observations) on the basis of variables where the data is arranged in ascending or descending order. The default sorting order of SAS is ascending, but we can sort data values also in descending order and as per our choice, by using "by statement." On using "by statement" SAS produces results on the basis of subsets of data values. The sorting of variables helps in better analysis and gives the best result.

Sorting in SAS

Note: SAS is not case sensitive language.

Syntax:

SAS PROC Sort default (Ascending Order)

Sorting | Ascending Order

When we want to sort data by a single variable and without mentioning sorting order (ascending or descending), then the SAS follows default sorting order, i.e. ascending.

Let's understand via an example:

In the following example, we are taking data of students of a class. The data of students include their student ID, name, sex, age, weight and height and for sorting, we are using a single variable that is weight. As a result, it will sort data on the basis of ascending order of student's weight.

Run the above code in SAS studio:

Sorting in SAS

Output:

Sorting in SAS

You can see in the output table, SAS has sorted data according to the ascending order of the weight.

Sorting in Descending Order

Sorting | Descending Order

If you need to sort in descending order, then you have to mention it with By statement and variable.

Syntax:

Let's understand via an example:

Run the above code in SAS studio:

Sorting in SAS

Output:

Sorting in SAS

You can see in the output table, SAS has sorted data according to the descending order of the age.

Sorting by By Statement

Sorting | By Statement

If you need to sort the data by applying the procedure (PROC) for each subset of data values, use the By Statement. You can apply it to variables that have data values which can be divided into several parts. We can apply sorting on multiple variable simultaneously.

Syntax:

Let's understand via an example:

In the example, we are sorting on the basis of the sex variable, which contains data values that can be divided into two parts, i.e. male and female.

Run the above code in SAS studio:

Sorting in SAS

Output:

Sorting in SAS

You can see in the output table that SAS has sorted the data according to sex. In the first table, the data is sorted according to the female (sex = f) and in the second table according to the male (sex = m).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA