options ls=80 nodate;

data pigs;
input diet $ gain init;
x1=(diet=
'a'); x2=(diet='b'); x3=(diet='c'); x4=(diet='d');
cards;
a 60.8 663.3
a 50.7 623.3
a 65.5 689.5
a 58.6 665.7
a 61.7 605.9
b 68.7 517.2
b 67.7 589.3
b 74.0 596.0
b 66.3 589.3
b 69.8 521.1
c 102.6 538.3
c 102.1 512.8
c 100.2 558.2
c 96.5 499.3
d 87.9 555.4
d 84.2 469.2
d 83.1 454.8
d 85.7 505.3
d 90.3 615.5
;

proc print;
title 'pig weight gain data';
run;

/* Example 8.6: Fitting a one-way ANOVA (ignoring initial weight) */

proc glm;
class diet;
model gain=diet/solution;
title 'one-way anova';
run;

/* Example 8.7: Fitting an ANCOVA model (incorporates initial weight)
Note: 'cldiff' gives the confidence intervals for the differences based on the
adjusted treatment means.
Note: 'cl' gives the confidence intervals for the treatment means based on the
adjusted treatment means.
Note: This code is also used for Examples 8.8-8.10. */

proc glm;
class diet;
model gain=diet init/solution; /* fits ANCOVA model */
means diet; /* gives unadjusted treatment means */
lsmeans diet/stderr pdiff cl; /* gives adjusted treatment means and CIs*/
title 'ANCOVA results';
run;

/* This means procedure will give \overline{x}_{++}, the overall mean of all the x's.
This is used in Example 8.8.*/

proc means;
var init;
run;

/* Fitting regression model to illustrate that the ANCOVA model is just a special
regression model; note that running both of these models gives the same ANOVA table
(and that it is also the same as the ANCOVA model) */

proc glm;
model gain = x1 x2 x3 init; /* means reparameterisation */
run;
proc glm;
model gain = x1 x2 x3 x4 init/solution; /* effects reparameterisation */
run;

/* Example 8.11: Fitting an ANCOVA model with different slopes; note that doing
this is achieved by adding the interaction term 'diet*init'. I also asked for the
adjusted treatment means (assuming unequal slopes) and the CI's.*/

proc glm;
class diet;
model gain=diet init diet*init/solution;
means diet;
lsmeans diet/stderr pdiff cl;
title 'ANCOVA with unequal slopes';
run;