SAS: How to Use PROC FREQ by Group

You can use the following basic syntax to calculate frequencies by group in SAS:

proc freq data=my_data;
    by var1;
    tables var2;

This particular syntax creates a frequency table for the values of the variable called var2, grouped by the variable called var1.

The following example shows how to use this syntax in practice.

Example: Using Proc FREQ by Group in SAS

Suppose we have the following dataset in SAS:

/*create dataset*/
data my_data;
    input team $ position $ points;
A Guard 22
A Guard 20
A Guard 30
A Forward 14
A Forward 11
B Guard 12
B Guard 22
B Forward 30
B Forward 9
B Forward 12
B Forward 25

/*view dataset*/
proc print data=my_data;

We can use the following PROC FREQ statement to calculate the frequency of the position values, grouped by team:

/*calculate frequency of position, grouped by team*/
proc freq data = my_data;
    by team;
    tables position;

PROC FREQ by group in SAS

The output displays the frequency of the values for the position variable, grouped by the team variable.

For example, we can see:

  • The value “Forward” occurred 2 times for team A.
  • The value “Guard” occurred 3 times for team A.
  • The value “Forward” occurred 4 times for team B.
  • The value “Guard” occurred 2 times for team B.

Note that in this example, we used the tables statement to calculate the frequencies for just one variable, but we could type out the names of multiple variables to calculate frequencies for more than one variable.

Note: You can find the complete documentation for PROC FREQ here.

Additional Resources

The following tutorials explain how to perform other common tasks in SAS:

How to Use Proc Summary in SAS
How to Use Proc Tabulate in SAS
How to Use Proc Rank in SAS

Leave a Reply

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