How to Create Frequency Tables in SAS (With Examples)


You can use proc freq in SAS to quickly create frequency tables for one or more variables in a dataset.

The following examples show how to use this procedure with the SAS built-in dataset called BirthWgt, 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;

Example 1: Frequency Table for One Variable

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

The output table contains four columns:

  • Frequency: The total number of observations that fell in a certain category.
  • Percent: The percentage of total observations that fell in a certain category.
  • Cumulative Frequency: The total number of observations that have been accounted for up to and including the current row.
  • Cumulative Percent: The cumulative percentage of total observations that have been accounted for up to and including the current row.

For example, from the output table we can see:

  • The total number of Hispanic mothers was 22,139.
  • The percentage of total mothers who were Hispanic was 22.14%.
  • The total number of mothers who were Asian, Black, or Hispanic was 41,496.
  • The cumulative percentage of mothers who were Asian, Black, or Hispanic was 41.50%.

Example 2: Frequency Table for One Variable (Sorted)

By default, frequency tables are sorted in alphabetical order based on the category names. However, we can use the order function to  sort the categories by frequency instead:

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

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

Example 3: Frequency Table for One Variable (Include Missing Values)

By default, missing values are not included in frequency tables.

However, we can use the missing command to tell SAS to include a row to count the frequency of missing values:

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

Since no additional row was added to the frequency table, this tells us that there were no missing values for Race in the original dataset.

Example 4: Frequency Table for Multiple Variables

To create a frequency table for multiple variables at once, we can simply include multiple variable names in the tables argument.

For example, we can use the following code to create a frequency table for both Race and AgeGroup:

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

We can see that a frequency table was created for both variables.

Featured Posts

One Reply to “How to Create Frequency Tables in SAS (With Examples)”

  1. Hey Zach,
    I am having a tough time with a data set. I have 192 rows of data, my 3 vars are Year Month and GasPrice, I want to output a data set that shows each year (2003 – 2018) in new Q1 – Q4 variables where I want to display months 1-3 in Q1 4-6 in Q2 7-9 in Q3 and 9 – 12 in Q4, Months are already assigned values 1-12:

    Total rows: 192Total columns: 3
    Year Month GasPrice
    1 2003 1 1.473
    2 2003 2 1.641
    3 2003 3 1.748
    4 2003 4 1.659
    5 2003 5 1.542
    6 2003 6 1.514
    7 2003 7 1.524
    8 2003 8 1.628
    9 2003 9 1.728
    10 2003 10 1.603
    11 2003 11 1.535
    12 2003 12 1.494
    13 2004 1 1.592
    14 2004 2 1.672
    15 2004 3 1.766
    16 2004 4 1.833
    17 2004 5 2.009
    >
    192 2018 12 3.012
    I am hoping it can look like:
    Year Q1_ GasPr Q2_GasPr Q3_GasPr Q4_GasPr
    2003 #.## #.## #.## #.##
    2004
    >>>
    20018

    I cannot seem to make format yyq. work or the tens of other functions or codes. I used an IF THEN to get some kind of database, but I still have 192 rows and a lot of empty/ ( . ) values. And nowhere near the data set I want. Any suggestions would be very appreciated.

Leave a Reply

Your email address will not be published. Required fields are marked *