SAS FAQ
How can I do ANOVA contrasts?

Let's use an example data set called crf24.
data crf24;
 input y a b;
 cards;
3 1 1
4 1 2
7 1 3
7 1 4
1 2 1
2 2 2
5 2 3
10 2 4
6 1 1
5 1 2
8 1 3
8 1 4
2 2 1
3 2 2
6 2 3
10 2 4
3 1 1
4 1 2
7 1 3
9 1 4
2 2 1
4 2 2
5 2 3
9 2 4
3 1 1
3 1 2
6 1 3
8 1 4
2 2 1
3 2 2
6 2 3
11 2 4
;
run;
These are data from a 2 by 4 factorial design.  The variable y is the dependent variable.  The variable a is an independent variable with two levels while b is an independent variable with four levels.

Using the contrast statement in a one-way ANOVA

proc glm data = crf24;
 class b;
 model y = b;
run;
quit;
The GLM Procedure

    Class Level Information

Class         Levels    Values

b                  4    1 2 3 4

Number of observations    32

Dependent Variable: y

                                        Sum of
Source                      DF         Squares     Mean Square    F Value    Pr > F

Model                        3     194.5000000      64.8333333      44.28    <.0001

Error                       28      41.0000000       1.4642857

Corrected Total             31     235.5000000

R-Square     Coeff Var      Root MSE        y Mean

0.825902      22.51306      1.210077      5.375000

Source                      DF       Type I SS     Mean Square    F Value    Pr > F

b                            3     194.5000000      64.8333333      44.28    <.0001


Source                      DF     Type III SS     Mean Square    F Value    Pr > F

b                            3     194.5000000      64.8333333      44.28    <.0001
proc means data = crf24 mean;
 class b;
 var y;
run;
The MEANS Procedure

       Analysis Variable : y

                  N
           b    Obs            Mean
-----------------------------------
           1      8       2.7500000

           2      8       3.5000000

           3      8       6.2500000

           4      8       9.0000000
-----------------------------------
It is quite clear that there is a significant overall F for the independent variable b.  Now let's devise some contrast that we can test:
1) group 3 versus group 4
2) the average of groups 1 and 2 versus the average of groups 3 and 4
3) the average of groups 1, 2 and 3 versus group 4.
proc glm data = 'd:\crf24';
  class b;
  model y = b;
  means b /deponly;
  contrast 'Compare 3rd & 4th grp' b 0 0 1 -1;
  contrast 'Compare 1st & 2nd with 3rd & 4th grp' b 1 1 -1 -1;
  contrast 'Compare 1st, 2nd & 3rd grps with 4th grp' b 1 1 1 -3;
run;
quit;
The GLM Procedure

    Class Level Information

Class         Levels    Values

b                  4    1 2 3 4

Number of observations    32

Dependent Variable: y

                                        Sum of
Source                      DF         Squares     Mean Square    F Value    Pr > F

Model                        3     194.5000000      64.8333333      44.28    <.0001

Error                       28      41.0000000       1.4642857

Corrected Total             31     235.5000000

R-Square     Coeff Var      Root MSE        y Mean

0.825902      22.51306      1.210077      5.375000


Source                      DF       Type I SS     Mean Square    F Value    Pr > F

b                            3     194.5000000      64.8333333      44.28    <.0001


Source                      DF     Type III SS     Mean Square    F Value    Pr > F

b                            3     194.5000000      64.8333333      44.28    <.0001

Level of           --------------y--------------
b            N             Mean          Std Dev

1            8       2.75000000       1.48804762
2            8       3.50000000       0.92582010
3            8       6.25000000       1.03509834
4            8       9.00000000       1.30930734

Dependent Variable: y

Contrast                                      DF   Contrast SS   Mean Square  F Value  Pr > F

Compare 3rd & 4th grp                          1    30.2500000    30.2500000    20.66  <.0001
Compare 1st & 2nd with 3rd & 4th grp           1   162.0000000   162.0000000   110.63  <.0001
Compare 1st, 2nd & 3rd grps with 4th grp       1   140.1666667   140.1666667    95.72  <.0001

Using the contrast statement in a two-way ANOVA

Now let's try the same contrasts on b but in a two-way ANOVA.

proc glm data = 'd:\crf24';
  class a b;
  model y = a b a*b;
  contrast 'Compare 3rd & 4th grp' b 0 0 1 -1;
  contrast 'Compare 1st & 2nd with 3rd & 4th grp' b 1 1 -1 -1;
  contrast 'Compare 1st, 2nd & 3rd grps with 4th grp' b 1 1 1 -3;
run;
quit;
The GLM Procedure

    Class Level Information

Class         Levels    Values

a                  2    1 2

b                  4    1 2 3 4


Number of observations    32
Dependent Variable: y

                                        Sum of
Source                      DF         Squares     Mean Square    F Value    Pr > F

Model                        7     217.0000000      31.0000000      40.22    <.0001

Error                       24      18.5000000       0.7708333

Corrected Total             31     235.5000000

R-Square     Coeff Var      Root MSE        y Mean

0.921444      16.33435      0.877971      5.375000

Source                      DF       Type I SS     Mean Square    F Value    Pr > F

a                            1       3.1250000       3.1250000       4.05    0.0554
b                            3     194.5000000      64.8333333      84.11    <.0001
a*b                          3      19.3750000       6.4583333       8.38    0.0006

Source                      DF     Type III SS     Mean Square    F Value    Pr > F

a                            1       3.1250000       3.1250000       4.05    0.0554
b                            3     194.5000000      64.8333333      84.11    <.0001
a*b                          3      19.3750000       6.4583333       8.38    0.0006

Contrast                                      DF   Contrast SS   Mean Square  F Value  Pr > F

Compare 3rd & 4th grp                          1    30.2500000    30.2500000    39.24  <.0001
Compare 1st & 2nd with 3rd & 4th grp           1   162.0000000   162.0000000   210.16  <.0001
Compare 1st, 2nd & 3rd grps with 4th grp       1   140.1666667   140.1666667   181.84  <.0001

Note that the F-ratios in these contrasts are larger than the F-ratios in the one-way ANOVA example. This is because the two-way ANOVA has a smaller mean square residual than the one-way ANOVA.

How to cite this page

Report an error on this page or leave a comment

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.