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

FREE Online Courses: Elevate Skills, Zero Cost. Enroll Now!

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;

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;

"<yoastmark

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

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

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.

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

follow dataflair on YouTube

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

  2. Piyush Gupta says:

    Can we sort by a different variable first (say Age) and then while printing the results use a different variable in by (say Sex) so that we have two groups of Male and Female population sorted according to their Age?

Leave a Reply

Your email address will not be published. Required fields are marked *