UCLA Academic Technology Services HomeServicesClassesContactJobs
Search

SAS Textbook Examples
An Introduction to Categorical Analysis by Alan Agresti
Chapter 3: Three-way Contingency Tables

Inputting the Death Penalty data, table 3.1.2, p. 54.
data death;
  input victim defendant deathp count;
cards;
1  1  1   53
1  1  2  414
1  2  1   11
1  2  2   37
2  1  1    0
2  1  2   16
2  2  1    4
2  2  2  139
;
run;
proc format;
  value defendant 1='white' 2='black';
  value victim 1='white' 2='black';
  value deathp 1='Yes' 2='No';
run;
proc freq data = death;
  format defendant defendant. victim victim. deathp deathp.;
  weight count;
  by victim;
  table defendant*deathp/norow nocol ;
  exact or;
run;
victim=white
The FREQ Procedure

Table of defendant by deathp
defendant     deathp

Frequency|
Percent  |Yes     |No      |  Total
---------+--------+--------+
white    |     53 |    414 |    467
         |  10.29 |  80.39 |  90.68
---------+--------+--------+
black    |     11 |     37 |     48
         |   2.14 |   7.18 |   9.32
---------+--------+--------+
Total          64      451      515
            12.43    87.57   100.00

Statistics for Table of defendant by deathp
           Estimates of the Relative Risk (Row1/Row2)

Type of Study                   Value       95% Confidence Limits
-----------------------------------------------------------------
Case-Control (Odds Ratio)      0.4306        0.2072        0.8947
Cohort (Col1 Risk)             0.4952        0.2780        0.8822
Cohort (Col2 Risk)             1.1501        0.9824        1.3464

  Odds Ratio (Case-Control Study)
-----------------------------------
Odds Ratio                   0.4306

Asymptotic Conf Limits
95% Lower Conf Limit         0.2072
95% Upper Conf Limit         0.8947

Exact Conf Limits
95% Lower Conf Limit         0.2003
95% Upper Conf Limit         0.9956

Sample Size = 515
victim=black
The FREQ Procedure

Table of defendant by deathp
defendant     deathp

Frequency|
Percent  |Yes     |No      |  Total
---------+--------+--------+
white    |      0 |     16 |     16
         |   0.00 |  10.06 |  10.06
---------+--------+--------+
black    |      4 |    139 |    143
         |   2.52 |  87.42 |  89.94
---------+--------+--------+
Total           4      155      159
             2.52    97.48   100.0

Statistics for Table of defendant by deathp
           Estimates of the Relative Risk (Row1/Row2)

Type of Study                   Value       95% Confidence Limits
-----------------------------------------------------------------
Cohort (Col2 Risk)             1.0288        1.0006        1.0578

One or more risk estimates not computed --- zero cell.

Sample Size = 159
Inputting the clinic data and obtaining table 3.2 and odds ratios, p. 58-59.
data temp;
  input clinic treat response count;
cards;
1  1  1  18
1  1  2  12
1  2  1  12
1  2  2   8
2  1  1   2
2  1  2   8
2  2  1   8
2  2  2  32
;
run;
proc format;
  value treat 1='A' 2='B';
  value response 1='Success' 2='Failure';
run;
proc freq data = temp;
  format treat treat. response response.;
 weight count;
 table clinic*treat*response/norow nocol nopercent;
 exact or;
run;
The FREQ Procedure

Table 1 of treat by response
Controlling for clinic=1
treat     response

Frequency|Success |Failure |  Total
---------+--------+--------+
A        |     18 |     12 |     30
---------+--------+--------+
B        |     12 |      8 |     20
---------+--------+--------+
Total          30       20       50

Statistics for Table 1 of treat by response
Controlling for clinic=1
           Estimates of the Relative Risk (Row1/Row2)

Type of Study                   Value       95% Confidence Limits
-----------------------------------------------------------------
Case-Control (Odds Ratio)      1.0000        0.3151        3.1738
Cohort (Col1 Risk)             1.0000        0.6300        1.5872
Cohort (Col2 Risk)             1.0000        0.5001        1.9996

  Odds Ratio (Case-Control Study)
-----------------------------------
Odds Ratio                   1.0000

Asymptotic Conf Limits
95% Lower Conf Limit         0.3151
95% Upper Conf Limit         3.1738

Exact Conf Limits
95% Lower Conf Limit         0.2669
95% Upper Conf Limit         3.6617

Sample Size = 50
The FREQ Procedure

Table 2 of treat by response
Controlling for clinic=2

treat     response

Frequency|Success |Failure |  Total
---------+--------+--------+
A        |      2 |      8 |     10
---------+--------+--------+
B        |      8 |     32 |     40
---------+--------+--------+
Total          10       40       50
Statistics for Table 2 of treat by response
Controlling for clinic=2
           Estimates of the Relative Risk (Row1/Row2)

Type of Study                   Value       95% Confidence Limits
-----------------------------------------------------------------
Case-Control (Odds Ratio)      1.0000        0.1769        5.6541
Cohort (Col1 Risk)             1.0000        0.2501        3.9984
Cohort (Col2 Risk)             1.0000        0.7072        1.4141
  Odds Ratio (Case-Control Study)
-----------------------------------
Odds Ratio                   1.0000

Asymptotic Conf Limits
95% Lower Conf Limit         0.1769
95% Upper Conf Limit         5.6541

Exact Conf Limits
95% Lower Conf Limit         0.0872
95% Upper Conf Limit         6.5993

Sample Size = 50
Inputting the Chinese Smoking and Lung Cancer data, table 3.3, p. 60.
data cmh;
input center smoke cancer count @@;
cards;
1 1 1 126   1 1 2 100   1 2 1  35   1 2 2  61
2 1 1 908   2 1 2 688   2 2 1 497   2 2 2 807
3 1 1 913   3 1 2 747   3 2 1 336   3 2 2 598
4 1 1 235   4 1 2 172   4 2 1  58   4 2 2 121
5 1 1 402   5 1 2 308   5 2 1 121   5 2 2 215
6 1 1 182   6 1 2 156   6 2 1  72   6 2 2  98
7 1 1  60   7 1 2  99   7 2 1  11   7 2 2  43
8 1 1 104   8 1 2  89   8 2 1  21   8 2 2  36
;
run;
The Mantel-Haenszel odds ratio and the Breslow-Day statistic, p. 62-63.
proc freq data=cmh;
  weight count;
  table center*smoke*cancer/ relrisk cmh norow nocol nopercent ;
  ods output RelativeRisks=temp;
run;
Table 1 of smoke by cancer
Controlling for center=1

smoke     cancer

Frequency|       1|       2|  Total
---------+--------+--------+
       1 |    126 |    100 |    226
---------+--------+--------+
       2 |     35 |     61 |     96
---------+--------+--------+
Total         161      161      322


Statistics for Table 1 of smoke by cancer
Controlling for center=1

           Estimates of the Relative Risk (Row1/Row2)

Type of Study                   Value       95% Confidence Limits
-----------------------------------------------------------------
Case-Control (Odds Ratio)      2.1960        1.3432        3.5902
Cohort (Col1 Risk)             1.5292        1.1460        2.0406
Cohort (Col2 Risk)             0.6964        0.5641        0.8596

Sample Size = 322


Table 2 of smoke by cancer
Controlling for center=2

smoke     cancer

Frequency|       1|       2|  Total
---------+--------+--------+
       1 |    908 |    688 |   1596
---------+--------+--------+
       2 |    497 |    807 |   1304
---------+--------+--------+
Total        1405     1495     2900

Statistics for Table 2 of smoke by cancer
Controlling for center=2

           Estimates of the Relative Risk (Row1/Row2)

Type of Study                   Value       95% Confidence Limits
-----------------------------------------------------------------
Case-Control (Odds Ratio)      2.1430        1.8457        2.4881
Cohort (Col1 Risk)             1.4927        1.3762        1.6191
Cohort (Col2 Risk)             0.6966        0.6491        0.7476

Sample Size = 2900


Table 3 of smoke by cancer
Controlling for center=3

smoke     cancer

Frequency|       1|       2|  Total
---------+--------+--------+
       1 |    913 |    747 |   1660
---------+--------+--------+
       2 |    336 |    598 |    934
---------+--------+--------+
Total        1249     1345     2594


Statistics for Table 3 of smoke by cancer
Controlling for center=3

           Estimates of the Relative Risk (Row1/Row2)

Type of Study                   Value       95% Confidence Limits
-----------------------------------------------------------------
Case-Control (Odds Ratio)      2.1753        1.8445        2.5654
Cohort (Col1 Risk)             1.5289        1.3889        1.6829
Cohort (Col2 Risk)             0.7028        0.6542        0.7551

Sample Size = 2594


Table 4 of smoke by cancer
Controlling for center=4

smoke     cancer

Frequency|       1|       2|  Total
---------+--------+--------+
       1 |    235 |    172 |    407
---------+--------+--------+
       2 |     58 |    121 |    179
---------+--------+--------+
Total         293      293      586


Statistics for Table 4 of smoke by cancer
Controlling for center=4

           Estimates of the Relative Risk (Row1/Row2)

Type of Study                   Value       95% Confidence Limits
-----------------------------------------------------------------
Case-Control (Odds Ratio)      2.8503        1.9695        4.1252
Cohort (Col1 Risk)             1.7820        1.4196        2.2368
Cohort (Col2 Risk)             0.6252        0.5369        0.7280

Sample Size = 586


Table 5 of smoke by cancer
Controlling for center=5

smoke     cancer

Frequency|       1|       2|  Total
---------+--------+--------+
       1 |    402 |    308 |    710
---------+--------+--------+
       2 |    121 |    215 |    336
---------+--------+--------+
Total         523      523     1046

Statistics for Table 5 of smoke by cancer
Controlling for center=5

           Estimates of the Relative Risk (Row1/Row2)

Type of Study                   Value       95% Confidence Limits
-----------------------------------------------------------------
Case-Control (Odds Ratio)      2.3191        1.7745        3.0309
Cohort (Col1 Risk)             1.5723        1.3446        1.8384
Cohort (Col2 Risk)             0.6779        0.6036        0.7615

Sample Size = 1046


Table 6 of smoke by cancer
Controlling for center=6

smoke     cancer

Frequency|       1|       2|  Total
---------+--------+--------+
       1 |    182 |    156 |    338
---------+--------+--------+
       2 |     72 |     98 |    170
---------+--------+--------+
Total         254      254      508


Statistics for Table 6 of smoke by cancer
Controlling for center=6

           Estimates of the Relative Risk (Row1/Row2)

Type of Study                   Value       95% Confidence Limits
-----------------------------------------------------------------
Case-Control (Odds Ratio)      1.5880        1.0948        2.3032
Cohort (Col1 Risk)             1.2714        1.0396        1.5548
Cohort (Col2 Risk)             0.8006        0.6736        0.9517

Sample Size = 508


Table 7 of smoke by cancer
Controlling for center=7

smoke     cancer

Frequency|       1|       2|  Total
---------+--------+--------+
       1 |     60 |     99 |    159
---------+--------+--------+
       2 |     11 |     43 |     54
---------+--------+--------+
Total          71      142      213


Statistics for Table 7 of smoke by cancer
Controlling for center=7

           Estimates of the Relative Risk (Row1/Row2)

Type of Study                   Value       95% Confidence Limits
-----------------------------------------------------------------
Case-Control (Odds Ratio)      2.3691        1.1351        4.9447
Cohort (Col1 Risk)             1.8525        1.0541        3.2557
Cohort (Col2 Risk)             0.7819        0.6523        0.9373

Sample Size = 213


Table 8 of smoke by cancer
Controlling for center=8

smoke     cancer

Frequency|       1|       2|  Total
---------+--------+--------+
       1 |    104 |     89 |    193
---------+--------+--------+
       2 |     21 |     36 |     57
---------+--------+--------+
Total         125      125      250

Statistics for Table 8 of smoke by cancer
Controlling for center=8

           Estimates of the Relative Risk (Row1/Row2)

Type of Study                   Value       95% Confidence Limits
-----------------------------------------------------------------
Case-Control (Odds Ratio)      2.0032        1.0906        3.6796
Cohort (Col1 Risk)             1.4626        1.0163        2.1050
Cohort (Col2 Risk)             0.7301        0.5686        0.9377

Sample Size = 250

Summary Statistics for smoke by cancer
Controlling for center

  Cochran-Mantel-Haenszel Statistics (Based on Table Scores)

Statistic    Alternative Hypothesis    DF       Value      Prob
---------------------------------------------------------------
    1        Nonzero Correlation        1    280.1375    <.0001
    2        Row Mean Scores Differ     1    280.1375    <.0001
    3        General Association        1    280.1375    <.0001


            Estimates of the Common Relative Risk (Row1/Row2)

Type of Study     Method                  Value     95% Confidence Limits
-------------------------------------------------------------------------
Case-Control      Mantel-Haenszel        2.1745       1.9840       2.3832
  (Odds Ratio)    Logit                  2.1734       1.9829       2.3823

Cohort            Mantel-Haenszel        1.5192       1.4417       1.6008
  (Col1 Risk)     Logit                  1.5132       1.4362       1.5942

Cohort            Mantel-Haenszel        0.6999       0.6721       0.7290
  (Col2 Risk)     Logit                  0.7011       0.6734       0.7300


     Breslow-Day Test for
Homogeneity of the Odds Ratios
------------------------------
Chi-Square              5.1997
DF                           7
Pr > ChiSq              0.6356


Total Sample Size = 8419
proc print data=temp noobs;
  where studytype="Case-Control (Odds Ratio)";
  var table value;
  title 'Odds Ratio by Center';
run;
Odds Ratio by Center                                                          

          Table                   Value

Table 1 of smoke * cancer        2.1960
Table 2 of smoke * cancer        2.1430
Table 3 of smoke * cancer        2.1753
Table 4 of smoke * cancer        2.8503
Table 5 of smoke * cancer        2.3191
Table 6 of smoke * cancer        1.5880
Table 7 of smoke * cancer        2.3691
Table 8 of smoke * cancer        2.0032

How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California