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!
title "Ordered by Unformatted Values of TYPE (default)";
proc freq data=sashelp.cars; /*1*/
tables type;
run;
1
title "Ordered by Unformatted Values of TYPE (default)";
2
PROC FREQDATA=sashelp.cars; /*1*/
3
tables type;
4
RUN;
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!
title "Ordered by Descending Frequency of TYPE";
proc freq data=sashelp.cars order=freq; /*2*/
tables type;
run;
1
title "Ordered by Descending Frequency of TYPE";
2
PROC FREQDATA=sashelp.cars order=freq; /*2*/
3
tables type;
4
RUN;
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!
proc format; /*3*/
value FuelEff low-20="Low"
20<-30="Mid"
30<-high="High";
run;
1
PROC FORMAT; /*3*/
2
value FuelEff low-20="Low"
3
20<-30="Mid"
4
30<-high="High";
5
RUN;
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!
title "Ordered by Unformatted Values of MPG_Highway";
proc freq data=sashelp.cars; /*4*/
tables MPG_Highway;
format MPG_Highway FuelEff.;
run;
1
title "Ordered by Unformatted Values of MPG_Highway";
2
PROC FREQDATA=sashelp.cars; /*4*/
3
tables MPG_Highway;
4
FORMAT MPG_Highway FuelEff.;
5
RUN;
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!
title "Ordered by Formatted Values of MPG_Highway";
proc freq data=sashelp.cars order=formatted; /*5*/
tables MPG_Highway;
format MPG_Highway FuelEff.;
run;
1
title "Ordered by Formatted Values of MPG_Highway";
2
PROC FREQDATA=sashelp.cars order=formatted; /*5*/
3
tables MPG_Highway;
4
FORMAT MPG_Highway FuelEff.;
5
RUN;
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!
title "Ordered by Descending Frequency of Formatted values of MPG_Highway";
proc freq data=sashelp.cars order=freq; /*6*/
tables MPG_Highway;
format MPG_Highway FuelEff.;
run;
1
title "Ordered by Descending Frequency of Formatted values of MPG_Highway";
2
PROC FREQDATA=sashelp.cars order=freq; /*6*/
3
tables MPG_Highway;
4
FORMAT MPG_Highway FuelEff.;
5
RUN;
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.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.