options ps=60 ls=80 pageno=1 formdlim='_';

data battery;
input life mat $ temp $ temp2;
cards;
130 a1 b1 15
74 a1 b1 15
155 a1 b1 15
180 a1 b1 15
34 a1 b2 70
40 a1 b2 70
80 a1 b2 70
75 a1 b2 70
20 a1 b3 125
82 a1 b3 125
70 a1 b3 125
58 a1 b3 125
150 a2 b1 15
188 a2 b1 15
159 a2 b1 15
126 a2 b1 15
136 a2 b2 70
122 a2 b2 70
106 a2 b2 70
115 a2 b2 70
25 a2 b3 125
70 a2 b3 125
58 a2 b3 125
45 a2 b3 125
138 a3 b1 15
110 a3 b1 15
168 a3 b1 15
160 a3 b1 15
174 a3 b2 70
120 a3 b2 70
150 a3 b2 70
139 a3 b2 70
96 a3 b3 125
104 a3 b3 125
82 a3 b3 125
60 a3 b3 125
;

/* ANOVA table and interaction plot */

proc glm data=battery;
class mat temp;
model life = mat temp mat*temp;
means mat temp mat*temp;
output out=avgs p=ybar;
run;

proc gplot data=avgs;
plot ybar*mat=temp;
symbol1 v=triangle l=1 i=join cv=blue;
symbol2 v=circle l=1 i=join cv=orange;
symbol3 v=square l=1 i=join cv=purple;
run;

/* Tukey intervals for all 9 treatments */

proc glm;
class mat temp;
model life = mat temp mat*temp;
lsmeans mat temp mat*temp/pdiff cl adj=tukey;
run;

/* --Comparing material types at fixed temperature levels;
use a Bonferroni correction on the p-values within levels
of temperature if needed.
--I can't figure out how to get pairwise Tukey intervals using
lsmeans.
--Instead of "estimate," I could have substituted "contrast."*/

proc glm;
class mat temp;
model life = mat temp mat*temp;
estimate 'mat12_at_temp=15' mat -1 1 0 mat*temp -1 0 0 1 0 0 0 0 0;
estimate 'mat13_at_temp=15' mat -1 0 1 mat*temp -1 0 0 0 0 0 1 0 0;
estimate 'mat23_at_temp=15' mat 0 -1 1 mat*temp 0 0 0 -1 0 0 1 0 0;

estimate 'mat12_at_temp=70' mat -1 1 0 mat*temp 0 -1 0 0 1 0 0 0 0;
estimate 'mat13_at_temp=70' mat -1 0 1 mat*temp 0 -1 0 0 0 0 0 1 0;
estimate 'mat23_at_temp=70' mat 0 -1 1 mat*temp 0 0 0 0 -1 0 0 1 0;

estimate 'mat12_at_temp=120' mat -1 1 0 mat*temp 0 0 -1 0 0 1 0 0 0;
estimate 'mat13_at_temp=120' mat -1 0 1 mat*temp 0 0 -1 0 0 0 0 0 1;
estimate 'mat23_at_temp=120' mat 0 -1 1 mat*temp 0 0 0 0 0 -1 0 0 1;
run;

/* --This illustrates the use of orthogonal contasts to test for trends in
temperature for a fixed level of material. */

proc glm;
class mat temp;
model life = mat temp mat*temp;
contrast 'linear_for_mat1' temp -1 0 1 mat*temp -1 0 1 0 0 0 0 0 0;
contrast 'quadratic_for_mat1' temp 1 -2 1 mat*temp 1 -2 1 0 0 0 0 0 0;
run;

/* --These commands could be used IF material and temperature didn't interact.
--Here, we would just be comparing the means of material and the means of
temperature (separately). */

proc glm;
class mat temp;
model life = mat temp mat*temp;
means mat temp/tukey cldiff;
contrast 'temperature_linear' temp -1 0 1;
contrast 'quadratic_temperature' temp 1 -2 1;
run;