Data Set

Use PROC SORT to Order a SAS data set

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 by statement are obligatory. The data statement contains the data set to be sorted while the out statement contains the sorted data. If no out statement is specified, SAS sorts the original data set. The by 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 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.