You can test the equality of two proportions obtained from independent samples using the Pearson chi-square test. To perform this test, specify the CHISQ option in the TABLES statement in PROC FREQ, or – in SAS® Viya® – in PROC FREQTAB. This is equivalent to the well-known Z test for comparing two independent proportions.
Suppose you want to compare the proportions responding Yes to a question in independent samples of 100 men and 100 women. The number of men responding Yes is observed to be 30 and the number of women responding Yes was 45. The data can be arranged in a 2×2 table:
Response
Yes No Total
Gender Men 30 70 100
Women 45 55 100
It is assumed that each man in the sample of men had the same probability of responding yes. The same is assumed for the sample of women, though of course, the two probabilities might differ.
The following statements record the data in a SAS® data set. The programming statements following the INPUT statement create the Response variable with values Yes and No, create the Count variable that contains the count for the No level as the total count minus the number responding Yes, and output an observation for each Response level.
data YesNo; input Gender $ NumYes Total; Response="Yes"; Count=NumYes; output; Response="No "; Count=Total-NumYes; output; datalines; Men 30 100 Women 45 100 ;
PROC PRINT displays the resulting data set that contains one observation for each cell of the table.
proc print; id Gender Response; var Count; run;
Alternatively, you could create the same data set using the following code that enters the four cell counts of the Gender by Response table
data YesNo;
do Gender="Men","Women";
do Response="Yes","No";
input Count @@;
output;
end; end;
datalines;
30 70
45 55
;
You can now use PROC FREQ to display the data in table form and compute the chi-square test comparing the proportions. The WEIGHT statement specifies the COUNT variable containing the cell counts of the table. In the TABLE statement, the first variable defines the rows of the table, and the second variable defines the columns. The CHISQ option requests the chi-square test comparing the Gender proportions. The RISKDIFF option estimates the difference in the two proportions along with a confidence interval. The same syntax can be used in PROC FREQTAB in SAS Viya.
proc freq; weight Count; table Gender * Response / chisq riskdiff; run;
In the results, a table of statistics includes the Pearson chi-square test (labeled "Chi-Square"). The small p-value for the test (p=0.0285) indicates that the null hypothesis of equal proportions can be rejected and that the proportions are unequal. Note that the Pearson test is a test of the independence of the row and column variables – Gender and Response in this example. But this hypothesis can be shown to be equivalent to the hypothesis of equal row (or column) proportions.
Estimates of the difference in proportions (risk difference) with confidence intervals are provided in the Risk Estimates tables. Since Yes appears in Column 2, the "Column 2 Risk Estimates" table provides the desired estimates. The estimated difference in probabilities (Men in Row 1 - Women in Row 2) is -0.15 with 95% confidence limits (-0.2826, -0.0174). Since these limits do not include zero as a likely value of the population mean difference, the difference is significant at the 0.05 level.
When there are more variables involved, such as Race in addition to Gender in this example, the resulting table has more than two dimensions. Alternative methods can be used in this situation to compare the Gender proportions. One approach is to treat Race and any other variables as stratifying variables and to use the CMH option in FREQ or FREQTAB. For example, if the data contained a Race variable, the Gender proportions are compared by specifying Race and any other stratifying variables before Gender in the TABLE statement.
proc freq; weight Count; table Race * Gender * Response / cmh; run;
Three statistics are provided in the Cochran-Mantel-Haenszel (CMH) results (not shown). When there are only two levels in the variable preceding the response variable, all three statistics are the same. When there are more than two levels, the second statistic, labeled "Row Mean Scores Differ" tests the equality of the proportions adjusted for any stratifying variables.
Another alternative is to use a modeling approach such as by fitting a logistic model. In such models, estimates of the difference adjusted for the other variables can be done, as described in SAS KB0057272.
For the chi-square test to be valid, the cell counts must not be too small. The usual rule of thumb is that all cell counts should be at least 5, though this might be overly strict. When some cell counts are too small, you can use Fisher's exact test, which is also provided by the CHISQ option. The Fisher test, while more conservative, also shows a significant difference between the proportions (p=0.0405).
An exact confidence interval of the difference in probabilities can also be obtained by using the RISKDIFF(CL=EXACT) option and the EXACT statement in PROC FREQ or FREQTAB.
proc freq; weight Count; table Gender * Response / riskdiff(cl=exact); exact riskdiff; run;
Note that the exact method can require considerable time and memory. It should only be used when the large-sample confidence interval (above) is questionable due to small cell counts. There is no reason to question the validity of the large-sample interval in this table, and this is confirmed by the exact confidence limits being similar to the large-sample interval.
Notice that the Fisher test results include one-sided tests as well as the two-sided test that corresponds to the Pearson chi-square test. See SAS Note 22566 for interpretation of the one-sided Fisher test results. As noted above, the Pearson chi-square statistic is equivalent to the Z test statistic, which is also commonly used to test the equality of independent proportions. In fact, chi-square = Z2. Consequently, the p-value for the two-sided Z test is the same as for the chi-square test. And since the p-value for a two-sided test is double the one-sided p-value when the test statistic's distribution is symmetric (as is the normal distribution), you can get the one-sided p-value by simply halving the two-sided p-value. So, if your alternative hypothesis was that the proportion of men responding Yes is less than the proportion of women responding Yes, then the p-value for this one-sided test would be 0.0285/2 = 0.0143. Note that this is similar to the right-sided Fisher exact text p-value 0.0203.
SAS Note 24298 discusses assessing power and sample size needs and gives an example.
You can compare more than two proportions in the same way as above by adding a line in the DATA step for each proportion. The chi-square test (or second CMH test if there are stratifying variables) will now have more than one degree of freedom and a small p-value indicates that at least two of the proportions differ. See SAS KB0056907 for how to do multiple comparisons to see which proportions differ.