Published on :

Creating Frequency Charts with PROC FREQ

This code is also available in: Deutsch Español Français
This SAS© script demonstrates the use of the FREQ procedure (PROC FREQ) with the PLOT=FREQPLOT option to visualize frequency distributions. It presents several use cases illustrating the customization of frequency charts:
  • Generation of a simple frequency chart for a variable ('TYPE') to visualize the distribution of its values.
  • Sorting values by descending frequency (ORDER=FREQ) and changing the chart orientation to horizontal bars (ORIENT=HORIZONTAL) for better readability.
  • Creation of bivariate frequency charts (cross-tables) where a distinct chart is produced for each value of the first variable, allowing analysis of the second variable's distribution by group.
  • Use of the TWOWAY=STACKED option for stacked bar charts in the context of cross-tables, offering a comparative view of proportions.
Compatibility is ensured for SAS© 9 and SAS© Viya, and official SAS© documentation is referenced for more details on available options.
Data Analysis

Type : SASHELP


The script uses the integrated `sashelp.cars` dataset, which is a standard demonstration dataset available by default in the SAS environment and does not require external creation or management.

1 Code Block
PROC FREQ
Explanation :
This code block generates a simple frequency chart for the 'TYPE' variable from the `sashelp.cars` dataset. The `plots=freqplot` option requests the creation of a visual bar chart representing the frequency of each unique value of the 'TYPE' variable.
Copied!
1title "Frequency Distribution of TYPE";
2PROC FREQ DATA=sashelp.cars;
3 tables type / plots=freqplot;
4RUN;
2 Code Block
PROC FREQ
Explanation :
This block produces a frequency chart where the categories of the 'TYPE' variable are sorted by descending frequency (`order=freq` option). Additionally, the `plots=freqplot(orient=horizontal)` option changes the chart's orientation to display horizontal bars, potentially improving readability for certain distributions.
Copied!
1title "Descending Frequency Distribution of TYPE";
2PROC FREQ DATA=sashelp.cars order=freq;
3 tables type / plots=freqplot(orient=horizontal);
4RUN;
3 Code Block
PROC FREQ
Explanation :
This block illustrates the creation of a two-way frequency chart (a cross-table) for the 'ORIGIN' and 'TYPE' variables. The `plots=freqplot` option generates a distinct bar chart for each unique value of the 'ORIGIN' variable, showing the distribution of 'TYPE' within each origin.
Copied!
1title "Two-way Frequency Distribution of TYPE and ORIGIN";
2title2 "Separate Plots";
3PROC FREQ DATA=sashelp.cars;
4 tables origin*type / plots=freqplot;
5RUN;
4 Code Block
PROC FREQ
Explanation :
This last block presents an advanced two-way frequency chart with stacked bars (`twoway=stacked`) and a horizontal orientation. The bars are sorted by descending frequency (`order=freq`), and each stacked segment represents a 'TYPE' value within each 'ORIGIN', offering a visual comparison of the distributions of the two variables in an aggregated and detailed manner.
Copied!
1title "Two-way Descending Frequency Distribution of TYPE and ORIGIN";
2title2 "Stacked Bars";
3PROC FREQ DATA=sashelp.cars order=freq;
4 tables origin*type / plots=freqplot(twoway=stacked orient=horizontal);
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.