PROC FREQ is often the first choice when you want to generate basic frequency counts, but it is the last choice when it is compared to other statistical reporting procedures. People sometimes consider PROC FREQ last because they think they have little or no control over the appearance of the output. For example, PROC FREQ does not allow style options within the syntax, which the REPORT and TABULATE procedures do allow. Also, you cannot control the formats or headings with statements in the procedure step.
Sometimes, a simple frequency (via a one-way table) is all you want, and you don’t want to have to create an output data set just to add a format. A one-way table in PROC FREQ is unique also in that it includes a cumulative count and percent. These calculations cannot be done in other procedures without additional code or steps. However, there is a simple way to make some basic modifications to your table. By adding a PROC TEMPLATE step to modify either the Base.Freq.OneWayFreqs or the Base.Freq.OneWayList table template, you can change the formats of the statistics, move the label of the variable, change the labels of the statistics, and suppress the Frequency Missing row that appears below the table. These changes apply to all output destinations, including the traditional listing output. You can also use PROC TEMPLATE to make small modifications to the nodes that are generated by one-way tables in the table of contents for non-listing ODS destinations.
Customize the Formats and Labels for Statistics
If you want to modify the format for the statistics Frequency, Cumulative Frequency, Percent, and Cumulative Percent, you can use the FORMAT= option in PROC TEMPLATE. This option accepts SAS formats and user-defined formats. If you want to change the statistic heading, you can use the HEADER= option.
The following example uses both the FORMAT= and the HEADER= statements to format the statistics values and to change the Frequency heading to Count. This example also changes the Percent heading to Pct.
proc format; picture pctfmt (round) other='009.9%'; run; proc template; edit Base.Freq.OneWayList; edit frequency; header='Count'; format=comma12.; end; edit cumfrequency; format=comma12.; end; edit percent; header='Pct'; format=pctfmt.; end; edit cumpercent; format=pctfmt.; end; end; run; data class; set sashelp.class; wgt=1000; run; proc freq data=class; tables age; weight wgt; run;
This code generates the following table:
Move the Variable Label
If a variable has a label, it is centered, by default, at the top of the table, and the variable name appears above the column of values.
If you want to use the label instead of the name above the column, you can edit the HEADER= value in PROC TEMPLATE, as shown in the following example:
proc template; edit Base.Freq.OneWayList; edit h1; /* Set text to a blank instead of VARLABEL. */ text " "; end; edit FVariable; just=varjust; style=rowheader; id; generic; header=varlabel; end; end; run; proc freq data=sashelp.class; label age='Age in Years'; tables age; run;
This code generates the following table, which replaces the default location of the label with blank text and moves the label so that it is above the column of the variable's values, as shown in this example:
Suppress the Frequency Missing= Row
If a variable has missing values and you do not include the MISSING option in the TABLES statement of PROC FREQ, the output includes the frequency of missing values (Frequency Missing=) below the table with the number of missing values.
The following table shows the default output:
To suppress this line without including missing values in the table output, you can modify the table template, as follows:
data test; input c1 $; cards; a b . ; run; proc template; edit Base.Freq.OneWayFreqs; /* This is the default setting: */ /* define f2; */ /* text "Frequency Missing =" fMissing -12.99; */ /* print = pfoot2; */ /* end; */ edit f2; text " "; end; end; run; proc freq data=test; table c1; run;
The table that is generated by this code does not contain the Frequency Missing= row at the bottom of the table.
Customize the Table of Contents
For a one-way table, the default table of contents looks like this:
To control the first node, The Freq Procedure, you can use the ODS PROCLABEL statement. You can change the text of this node, but you cannot eliminate the node. You can remove the One-Way Frequencies node using PROC TEMPLATE with the CONTENTS= statement. The following example changes the text of the The Freq Procedure node, and it eliminates the One-Way Frequencies node.
proc template; edit Base.Freq.OneWayFreqs; contents=off; end; run; ods listing close; ods pdf file='test.pdf'; ods proclabel='My One-Way Table'; proc freq data=sashelp.class; tables age; run; ods pdf close; ods listing;
Here is the modified table of contents:
(For additional customization of the table of contents, you need to consider using the DOCUMENT procedure. The SAS Global Forum 2011 paper Let's Give 'Em Something to TOC about: Transforming the Table of Contents of Your PDF File, by Bari Lawhorn, contains useful information and examples that illustrate how to use PROC DOCUMENT, PROC TEMPLATE, and other SAS statements and options to customize your TOC.)
To restore the default table template, run the following code:
proc template; delete Base.Freq.OneWayFreqs; delete Base.Freq.OneWayList; run;
The examples above show how you actually can customize PROC FREQ one-way tables, with a little help from PROC TEMPLATE. PROC TEMPLATE offers several methods that make it easy to customize one-way, frequency-count tables that enhance table format and appearance.
Seeing the FREQ procedure's one-way tables in a new light was published on SAS Users.