How do I sort the output of a PROC FREQ in SAS?

The output of PROC FREQ in SAS can be sorted by using the ORDER= option. This option allows you to specify the order in which the output is sorted. You can sort by count, percent, or a user-defined variable. Additionally, you can specify the order of the sort (ascending or descending). This allows you to quickly and easily sort the output of PROC FREQ to get the desired view of the data.


You can use PROC FREQ with the ORDER=FREQ option in SAS to to create a frequency table in which the categories in the table are sorted based on frequency.

You can use the following syntax to do so:

proc freq data=my_data order=freq;
    tables my_variable;
run;

The following example shows how to use this syntax in practice.

Example: Use PROC FREQ with ORDER Option in SAS

For this example we will use the SAS built-in dataset called , which contains various characteristics for 100,000 mothers that recently gave birth.

We can use PROC PRINT to view the first 10 observations from this dataset:

/*view first 10 observations from BirthWgt dataset*/
proc print data=sashelp.BirthWgt (obs=10);

run;

We can use the following code to create a frequency table for the Race variable:

/*create frequency table for Race variable*/
proc freq data=sashelp.BirthWgt;
	tables Race;
run;

frequency table in SAS

Notice that the categories are currently sorted in alphabetical order.

To instead sort the categories by frequency, we can use the following syntax:

/*create frequency table for Race variable, sorted by frequency*/
proc freq data=sashelp.BirthWgt order=freq;
	tables Race;
run;

SAS PROC FREQ with ORDER option

Notice that the categories are now sorted based on frequency from highest to lowest.

However, you can use the following workaround with the PROC SORT statement to sort based on frequency from lowest to highest:

/*create frequency table and store results in Racefreq*/
proc freq data=sashelp.BirthWgt noprint;
   tables Race / out=Racefreq;
run;

/*sort Racefreq based on frequency from lowest to highest*/
proc sort data=Racefreq;
  by count;
run;

/*create new dataset with cumulative freq and cumulative percent*/
data freq_low_to_high;
  set Racefreq;
  cumcount + count;
  cumpercent + percent;
run;

/*view results*/
proc print data=freq_low_to_high;

SAS PROC FREQ sort ascending

Notice that the categories are now sorted based on frequency from lowest to highest.

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

x