/* Example of Three-Period Crossover MJ 32.3 */
options nocenter linesize=80 pagesize=50;
data cross;
   infile 'cross33.dat' firstobs = 2;
   input seq period trt $ u1 u2 u3 u4 u5 u6;
proc sort; by seq period;
data b; set cross;
   if seq = 1 or seq = 2 then do;
      carry = '0'; LA = 0; LB = 0; LC = 0;
      if period = 2 then do; carry = 'A'; LA = 1; end;
      if period = 3 then do; LA = 0;
         if seq = 1 then do; carry = 'B'; LB = 1; end;
         if seq = 2 then do; carry = 'C'; LC = 1; end;
      end;
   end;
   if seq = 3 or seq = 4 then do;
      carry = '0'; LA = 0; LB = 0; LC = 0;
      if period = 2 then do; carry = 'B'; LB = 1; end;
      if period = 3 then do; LB = 0;
         if seq = 3 then do; carry = 'A'; LA = 1; end;
         if seq = 4 then do; carry = 'C'; LC = 1; end;
      end;
   end;
   if seq = 5 or seq = 6 then do;
      carry = '0'; LA = 0; LB = 0; LC = 0;
      if period = 2 then do; carry = 'C'; LC = 1; end;
      if period = 3 then do; LC = 0;
         if seq = 5 then do; carry = 'A'; LA = 1; end;
         if seq = 6 then do; carry = 'B'; LB = 1; end;
      end;
   end;
   wpcarry = 'AB';
   if seq = 2 or seq = 5 then wpcarry = 'AC';
   if seq = 4 or seq = 6 then wpcarry = 'BC';
   y = u1; person = 1; output;
   y = u2; person = 2; output;
   y = u3; person = 3; output;
   y = u4; person = 4; output;
   y = u5; person = 5; output;
   y = u6; person = 6; output;
   drop u1 u2 u3 u4 u5 u6;
proc print;
proc glm;
   class seq wpcarry trt period carry person;
   model y = person(seq) trt period carry / solution;
   lsmeans trt period / stderr pdiff;

proc mixed data = b;
   class trt wpcarry seq period carry person;
   model y = trt period carry / ddfm = satterth;
   random person(seq);
   lsmeans trt carry / pdiff;
proc mixed data = b;
   class seq wpcarry trt period person;
   model y = trt LA LB LC period / ddfm = satterth;
   random person(seq);
   lsmeans trt period / pdiff;

/* now include whole plot carry */
proc glm;
   class seq wpcarry trt period carry person;
   model y = wpcarry person(seq*wpcarry) trt period carry / solution;
   lsmeans trt period / stderr pdiff;
proc mixed data = b;
   class trt wpcarry seq period carry person;
   model y = wpcarry trt period carry / ddfm = satterth;
   random person(seq*wpcarry);
   lsmeans trt carry / pdiff;
proc mixed data = b;
   class seq wpcarry trt period person;
   model y = wpcarry trt LA LB LC period / ddfm = satterth;
   random person(seq*wpcarry);
   lsmeans trt period / pdiff;
