How to use PROC FREQ with WHERE Statement in SAS programming language


You can use the following basic syntax to use a WHERE statement within PROC FREQ in SAS:

proc freq data=my_data;
    where var1 ='A';
    tables var2;
run;

This particular syntax creates a frequency table for the variable called var2 but only for the rows where var1 is equal to ‘A’.

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

Example: Using Proc FREQ with WHERE Statement in SAS

Suppose we have the following dataset in SAS:

/*create dataset*/
data my_data;
    input team $ position $ points;
    datalines;
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
;
run;

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

We can use the following PROC FREQ statement with a WHERE statement to calculate the frequency of values in the position column only for the rows where the team is equal to ‘A’:

/*calculate frequency of position where team is equal to 'A'*/
proc freq data=my_data;
    where team='A';
    tables position;
run;

The output displays the frequency of the values for the position variable only for the rows where the team is equal to ‘A.’

For example, we can see:

  • The value “Forward” occurs 2 times for team A.
  • The value “Guard” occurs 3 times for team A.

We can also use the OR and AND operators to specify multiple conditions within the WHERE statement.

For example, we can use the following code to calculate the frequency of values in the position column where the team is equal to ‘A’ and the position is equal to ‘Guard’:

/*calculate frequency of position where team is 'A' and position is 'Guard'*/
proc freq data=my_data;
    where team='A' and position='Guard';
    tables position;
run;

The output displays the frequency of the values for the position variable only for the rows where the team is equal to ‘A’ and the position is equal to ‘Guard.’

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

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

x