/* SAS code to analyze an r by c contingency table */ /* and do chi-squared test of independence */ /* We look at the snoring / heart disease data set: */ DATA snoreheart; INPUT heart $ snoring $ number; cards; yes never 24 yes occas 35 yes almostevery 51 no never 1355 no occas 603 no almostevery 416 ; run; /* We test for independence of the two classifications using */ /* the chisq option in PROC FREQ. */ /* In the TABLES statement below, the first variable listed is the row variable */ /* and the second variable listed is the column variable. */ PROC FREQ DATA = snoreheart ORDER=DATA; TABLES heart*snoring / chisq EXPECTED; WEIGHT number; run; /* The expected counts are given in each cell below the observed counts. */ /* P-value given as less than .0001. */ /**************************************************************/ /* SAS code to analyze an r by c contingency table */ /* and do chi-squared test of homogeneity */ /* We look at the voter survey data set: */ DATA voter; INPUT sample $ category $ number; cards; Upstate Approve 61 Upstate Neutral 50 Upstate Disapprove 39 Midlands Approve 56 Midlands Neutral 40 Midlands Disapprove 54 Coastal Approve 47 Coastal Neutral 37 Coastal Disapprove 66 ; run; /* We test for homogeneity using */ /* the chisq option in PROC FREQ. */ /* In the TABLES statement below, the first variable listed is the row variable */ /* and the second variable listed is the column variable. */ PROC FREQ DATA = voter ORDER=DATA; TABLES sample*category / chisq EXPECTED; WEIGHT number; run; /* The expected counts are given in each cell below the observed counts. */ /* The P-value is 0.027. */ /**************************************************************/ /* SAS code to do Fisher's exact test */ /* on a made-up small data set */ /* (3 by 3 table) */ DATA small; INPUT sample $ category $ number; cards; Upstate Approve 6 Upstate Neutral 5 Upstate Disapprove 3 Midlands Approve 5 Midlands Neutral 4 Midlands Disapprove 5 Coastal Approve 4 Coastal Neutral 3 Coastal Disapprove 6 ; run; /* We use PROC FREQ with the FISHER option. */ /* In the TABLES statement below, the first variable listed is the row variable */ /* and the second variable listed is the column variable. */ PROC FREQ DATA = small ORDER=DATA; TABLES sample*category / FISHER EXPECTED; WEIGHT number; run;