Published on :
Reporting SASHELP

Controlling the Sort Order in a Frequency Report

This code is also available in: Deutsch Español Français
Awaiting validation
The script demonstrates several methods for ordering a frequency report. It begins with the default order based on the internal values of the variable, then shows how to sort by descending frequency. Next, a custom format is created with PROC FORMAT to group values. The script then illustrates how sorting can be based on these formatted values or on the frequency of occurrences of these new formatted values.
Data Analysis

Type : SASHELP


The script exclusively uses the 'cars' table from the standard SASHELP library. No external data is needed.

1 Code Block
PROC FREQ
Explanation :
This block executes a FREQ procedure on the 'type' variable from the 'sashelp.cars' table. By default, the results are sorted according to the internal (unformatted) values of the 'type' variable.
Copied!
1title "Ordered by Unformatted Values of TYPE (default)";
2PROC FREQ DATA=sashelp.cars; /*1*/
3 tables type;
4RUN;
2 Code Block
PROC FREQ
Explanation :
This block uses the 'order=freq' option to sort the results of the FREQ procedure based on the descending frequency of each modality of the 'type' variable.
Copied!
1title "Ordered by Descending Frequency of TYPE";
2PROC FREQ DATA=sashelp.cars order=freq; /*2*/
3 tables type;
4RUN;
3 Code Block
PROC FORMAT Data
Explanation :
This block uses PROC FORMAT to create a custom format named 'FuelEff'. This format categorizes numerical values into three groups: 'Low', 'Mid', and 'High'.
Copied!
1PROC FORMAT; /*3*/
2 value FuelEff low-20="Low"
3 20<-30="Mid"
4 30<-high="High";
5RUN;
4 Code Block
PROC FREQ
Explanation :
This block applies the 'FuelEff.' format to the 'MPG_Highway' variable. The default sort order remains based on the internal (unformatted) values of 'MPG_Highway', although the format labels are displayed.
Copied!
1title "Ordered by Unformatted Values of MPG_Highway";
2PROC FREQ DATA=sashelp.cars; /*4*/
3 tables MPG_Highway;
4 FORMAT MPG_Highway FuelEff.;
5RUN;
5 Code Block
PROC FREQ
Explanation :
Thanks to the 'order=formatted' option, the results are now sorted according to the alphabetical order of the 'FuelEff' format labels applied to 'MPG_Highway' (thus 'High', 'Low', 'Mid').
Copied!
1title "Ordered by Formatted Values of MPG_Highway";
2PROC FREQ DATA=sashelp.cars order=formatted; /*5*/
3 tables MPG_Highway;
4 FORMAT MPG_Highway FuelEff.;
5RUN;
6 Code Block
PROC FREQ
Explanation :
This last block combines the application of the format with the 'order=freq' option. The results are sorted by descending frequency of the groups defined by the 'FuelEff' format.
Copied!
1title "Ordered by Descending Frequency of Formatted values of MPG_Highway";
2PROC FREQ DATA=sashelp.cars order=freq; /*6*/
3 tables MPG_Highway;
4 FORMAT MPG_Highway FuelEff.;
5RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.