How can I use Proc Sort in SAS, and what are some examples of using it? 2

How can I use Proc Sort in SAS, and what are some examples of using it?

Proc Sort is a SAS procedure that allows for the sorting of data within a SAS dataset. It can be used to rearrange the observations in a dataset according to specified variables, in either ascending or descending order. This can be useful for organizing data for analysis or for creating reports. Some examples of using Proc Sort include arranging data by date, alphabetizing names, or grouping data by category. It can also be used in conjunction with other SAS procedures to perform more complex data manipulations. Overall, Proc Sort is a powerful tool in SAS that aids in efficient data management and organization.

Use Proc Sort in SAS (With Examples)


You can use proc sort in SAS to order the observations in a dataset by one or more variables.

The following examples show how to use this procedure with the following dataset in SAS:

/*create dataset*/
data original_data;
    input team $ points rebounds;
    datalines;
A 12 8
A 12 7
A 14 5
A 23 9
A 20 12
A 11 7
A 14 7
B 20 2
B 20 5
B 29 4
B 14 7
B 19 8
B 17 9
B 30 9
;
run;

/*view dataset*/
proc printdata=original_data;

Example 1: Sort Observations Ascending

We can use proc sort to sort the observations in the dataset in ascending order (smallest to largest) based on the value in the points column:

/*sort by points ascending*/
proc sortdata=original_data out=data2;
    by points;
run;

/*view sorted dataset*/
proc printdata=data2;

SAS proc sort ascending

Notice that the observations are sorted in ascending order based on the value in the points column.

Example 2: Sort Observations Descending

We can use proc sort with the descending statement to sort the observations in the dataset in descending order (largest to smallest) based on the value in the points column:

/*sort by points descending*/
proc sortdata=original_data out=data3;
    bydescending points;
run;

/*view sorted dataset*/
proc printdata=data3;

SAS proc sort descending

Notice that the observations are sorted in ascending order based on the value in the points column.

Example 3: Sort Observations by Multiple Columns

We can use proc sort with multiple variables listed in the by statement to sort the observations in the dataset by multiple variables.

The following code shows how to sort the observations in the dataset by the value in the points column ascending, then by the value in the rebounds column ascending:

/*sort by points ascending, then by rebounds ascending*/
proc sortdata=original_data out=data4;
    by points rebounds;
run;

/*view sorted dataset*/
proc printdata=data4;

Notice that the observations are sorted by the value in the points column ascending, then by the value in the rebounds column ascending.

The following tutorials explain how to perform other common tasks in SAS:

Cite this article

stats writer (2024). How can I use Proc Sort in SAS, and what are some examples of using it?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-proc-sort-in-sas-and-what-are-some-examples-of-using-it/

stats writer. "How can I use Proc Sort in SAS, and what are some examples of using it?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-proc-sort-in-sas-and-what-are-some-examples-of-using-it/.

stats writer. "How can I use Proc Sort in SAS, and what are some examples of using it?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-proc-sort-in-sas-and-what-are-some-examples-of-using-it/.

stats writer (2024) 'How can I use Proc Sort in SAS, and what are some examples of using it?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-proc-sort-in-sas-and-what-are-some-examples-of-using-it/.

[1] stats writer, "How can I use Proc Sort in SAS, and what are some examples of using it?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I use Proc Sort in SAS, and what are some examples of using it?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top