# SAS Proc Sort Data Sets – Ascending, Descending & BY Statements

Today we will be learning about SAS PROC Sort Data Sets. We will then be looking at two ways of PROC sorting a dataset in SAS: SAS Default Sort and Reverse Sorting in SAS. We will also learn the BY statement that is very important while PROC Sorting data values in SAS Programming Language.

So let us begin with PROC Sorting Data Sets in SAS.

## Sorting in SAS

Sorting in SAS is a process of a simple arrangement where data arranges in ascending or descending sort order. The default order of sorting is ascending (SAS Sort in ascending). The sorting of variable results in better analysis.

Now let us look at the syntax of a SAS PROC SORT statement:

```proc sort data=<name of data>;
by <name of variable>;
run;```

Join DataFlair on Telegram!!

## SAS PROC Sort Ascending Order

The SAS Sorting by a single variable (default: Ascending Order).

```data one;
input studyid name \$ sex \$ age weight height;
cards;
1          Carol          f           22           120      64;
6          Julie          f           55           125      63;
2          Joe            m           34           130      68;
7          Kelsey         f           43           130      64;
4          Ann            f           29            .       65;
11         Tracee         f           21           110       .;
9          Bob            m           32           155      71;
3          Ed             m           40           120      69;
8          Karl           m           35           160      68;
10         Gayla          f           40           130      68;
5          John           m           17           175      73;
run;
proc sort data=one;
by weight;
run;
/*will sort data one by the variable weight in ascending order */
proc print data=one;
run;```

## SAS PROC Sort Descending Order

Sorting in Descending Order (reverse sorting)

Example:

``` proc sort data=one;
by  descending height;
run;
proc print data=one;
id studyid;
var name age height;
run;```

SAS PROC Sorting in Descending Order

## BY Statement

This statement instructs SAS to apply the procedure for each subset of data and can be applied to more than one SAS variable.

Syntax:

```proc <name of SAS Procedure> data=<name of data>;
<SAS Statements>
by <variable name>;
run;```

Example:

```/* First sort the data */
proc sort data=one;
by sex;
run;
proc print data=one;
by sex;
run;```

BY Statement

## Summary

So, today we looked at the two ways by which we can sort our data, which is either in ascending or in descending order.

We then looked at the BY statement in SAS through which we can apply sorting on multiple variables and it is an important statement in the proc sort statement. Furthermore, if you have any query feel free to ask in a comment section.

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google | Facebook

### 2 Responses

1. Neeraj says:

proc sort data=one;
by descending height;
run;
proc print data=one;
id studyid;
var name age height;
run;

In the above program what is the use of :
proc print data=one;
id studyid;

• Basavesh says:

if you pass any variable for id (keyword) then that variable becomes the index variable for your output else default index (observations) will be put by system.