In SAS you can order a data set using **proc sort**. The code below shows some examples how to use this procedure.

In **proc sort** the * data* and

*statement are obligatory. The*

**by***statement contains the data set to be sorted while the*

**data***statement contains the sorted data. If no*

**out***statement is specified, SAS sorts the original data set. The*

**out****statement contains the variable(s) by which the input data set will be sorted. The**

*by***statement always has one or more variables. By default, the**

*by***sort**procedure sorts the data set in

*ascending*order. However, you can sort the data set in descending order using the keyword

*descending.*The

*descending*keyword must be placed before the variable.

```
data work.ds_unsorted;
set sashelp.cars (keep= Make Model);
run;
/* Sort the data set by model (ascending) */
proc sort data=work.ds_unsorted
out=work.ds_sorted_model;
by model;
run;
/* Sort the data by model (descending) */
proc sort data=work.ds_unsorted
out=work.ds_sorted_model_desc;
by descending model;
run;
/* Sort the data by make and model (both ascending) */
proc sort data=work.ds_unsorted
out=work.ds_sorted_make_model;
by make model;
run;
```

You can find the official SAS documentation of the PROC SORT procedure here.

## 10 thoughts on “Use PROC SORT to Order a SAS data set”

Comments are closed.