Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

SAS dataset sorting


May 27, 2021 SAS


Table of contents


The dataset in the SAS can sort any variable that exists in it. T his helps in data analysis and performing other options, such as merging, etc. S orting can occur in any single variable as well as multiple variables. T he SAS procedure used to perform sorting in the SAS data set is called PROC SORT. T he sorted results are stored in the new dataset, and the original dataset remains unchanged.

Grammar

The basic syntax of sorting operations in the data set in SAS is:

PROC SORT DATA=original dataset OUT=Sorted dataset;
  BY variable name;

The following is a description of the parameters used:

  • The variable name is the column name that the sort occurs in.
  • The original dataset is the name of the dataset to sort.
  • The sorted dataset is the name of the sorted dataset.

Cases

Let's consider the following SAS dataset that contains employee details for your organization. W e can use the following code to sort payroll datasets.

DATA Employee; 
  INPUT empid name $ salary DEPT $ ; 
DATALINES; 
1 Rick 623.3	IT 		 
2 Dan 515.2 	OPS	
3 Mike 611.5 	IT 	
4 Ryan 729.1    HR 
5 Gary 843.25   FIN 
6 Tusar 578.6   IT 
7 Pranab 632.8  OPS
8 Rasmi 722.5   FIN 
;
RUN;

PROC SORT DATA=Employee OUT=Sorted_sal ;
  BY salary;
RUN ;
 
PROC PRINT DATA=Sorted_sal;
RUN ; 

When executing the above code, we can get the following output.

SAS dataset sorting

Reverse sorting

The default sort options are arranged in ascending order, which means that the observations are arranged from lower to higher values of the sort variable. B ut we may also want sorting to occur in ascending order.

Cases

In the following code, reverse sorting is achieved by using the DESCENDING statement.

DATA Employee; 
  INPUT empid name $ salary DEPT $ ; 
DATALINES; 
1 Rick 623.3	IT 		 
2 Dan 515.2 	OPS	
3 Mike 611.5 	IT 	
4 Ryan 729.1    HR 
5 Gary 843.25   FIN 
6 Tusar 578.6   IT 
7 Pranab 632.8  OPS
8 Rasmi 722.5   FIN 
;
RUN;

PROC SORT DATA=Employee OUT=Sorted_sal_reverse ;
  BY DESCENDING salary;
RUN ;
 
PROC PRINT DATA=Sorted_sal_reverse;
RUN ; 

When executing the above code, we can get the following output.

SAS dataset sorting

Sort multiple variables

By using them in BY statements, you can apply sorting to multiple variables. V ariables are sorted from left to right.

Cases

In the following code, the dataset is sorted first on the variable name and then on the variable name salary.

DATA Employee; 
  INPUT empid name $ salary DEPT $ ; 
DATALINES; 
1 Rick 623.3	IT 		 
2 Dan 515.2 	OPS	
3 Mike 611.5 	IT 	
4 Ryan 729.1    HR 
5 Gary 843.25   FIN 
6 Tusar 578.6   IT 
7 Pranab 632.8  OPS
8 Rasmi 722.5   FIN 
;
RUN;

PROC SORT DATA=Employee OUT=Sorted_dept_sal ;
  BY salary DEPT;
RUN ;
 
PROC PRINT DATA=Sorted_dept_sal;
RUN ; 

When executing the above code, we can get the following output.

SAS dataset sorting