UCLA Academic Technology Services HomeServicesClassesContactJobs
Search

SAS Code Fragment
Doubly multivariate anova in SAS

options nocenter ls=80;

DATA test;
  input  s trt pre_a pre_b post_a post_b pre_cov post_cov;
cards;
1.00 1.00 3.00 5.00 4.00 7.00 2.00 4.00
2.00 1.00 3.00 7.00 2.00 9.00 3.00 3.00
3.00 1.00 4.00 6.00 7.00 9.00 4.00 8.00
4.00 1.00 2.00 8.00 4.00 8.00 3.00 9.00
5.00 1.00 2.00 4.00 5.00 8.00 2.00 7.00
6.00 2.00 4.00 5.00 8.00 9.00 1.00 2.00
7.00 2.00 4.00 4.00 8.00 1.00 8.00 8.00
8.00 2.00 3.00 7.00 7.00 2.00 5.00 7.00
9.00 2.00 2.00 2.00 4.00 3.00 4.00 3.00
10.00 2.00 5.00 3.00 3.00 8.00 3.00 2.00
;
run;

* 1a. Doubly multivariate in SAS the easy way ;
Proc GLM Data=test;
  Class trt;
  Model pre_a post_a
        pre_b post_b = trt / nouni;
  repeated Response 2 identity, Time 2;
run;
quit;

title "1b. doubly multivariate anova using PROC GLM the hard way" ;
* need to specify terms manually to get trt, time, trt*time ;
* could assess if effect of trt*time differs by a vs b, see 2a ;
Proc GLM Data=test;
  Class trt;
  Model pre_a pre_b post_a post_b = trt / nouni;
  run;

  Title "1. Repeated Measures Main Effect of TRT";
  MANOVA H=trt M=pre_a+post_a, pre_b+post_b / printe printh;
  RUN;

  Title "2. Repeated Measures Main Effect of TIME";
  MANOVA H=intercept M=pre_a-post_a, pre_b-post_b / printe printh;
  run;

  Title "3. Repeated Measures Interaction TRT*TIME";
  MANOVA H=trt M=pre_a-post_a, pre_b-post_b / printe printh;
  run;

quit;

title "2a. doubly multivariate anova, assessing differential effects on DVs.";
* specifying within effects via M matrix to assess differential effects ;
* to see if effect of trt and trt*time differ by a vs b. ;
* see effects in 4 5 & 6 ;
Proc GLM Data=test;
  Class trt;
  Model pre_a pre_b post_a post_b = trt / nouni;
  run;

  Title "4. Repeated Measures Interaction TRT*avsb";
  MANOVA H=trt M=pre_a+post_a-pre_b-post_b / printe printh;
  run;

  Title "5. Repeated Measures Interaction TRT*TIME";
  MANOVA H=trt M=pre_a-post_a+pre_b-post_b / printe printh;
  run;

  Title "6. Repeated Measures Interaction TRT*TIME BY a vs b";
  MANOVA H=trt M=pre_a-post_a-pre_b+post_b / printe printh;
  run;
quit;

title "2b. doubly multivariate assessing differential effects on DVs." ;
Proc GLM Data=test;
  Class trt;
  Model pre_a pre_b post_a post_b = trt / nouni;
  Repeated time 2 PROFILE, ab 2 PROFILE / printe printm printh summary;
run;


title "3. doubly multivariate as a univariate" ;
Proc GLM Data=test;
  Class trt;
  Model pre_a pre_b post_a post_b = trt / nouni;
  Repeated time 2, ab 2 ; 
run;

* 4. univariate of b, adjusting for variable a. ;
Proc GLM Data=test;
  Class trt;
  Model pre_a post_a = trt pre_b post_b / nouni solution ;
  Repeated time 2 / printe printm printh summary ;
run;

data long;
  set test;

  time = 0;
  a = pre_a;
  b = pre_b;
  cov = pre_cov;
  if trt = 1 then pretrt = 1; else pretrt = -1;
  output;

  time = 1;
  a = post_a;
  b = post_b;
  cov = post_cov;
  if trt = 1 then pretrt = -1; else pretrt = 1;
  output;

  keep s trt time a b cov pretrt ;
run;

title "5. ancova with a constant covariate";
PROC MIXED data=long;
  class s trt time ;
  model a = trt time trt*time b / solution ;
  repeated time / subject=s type=un ;
run;

title "6. ancova, varying covariate, test differences in covariate";
* the effect of covariate varies across cells;
* Below is the code for testing the difference in the effect of covariate b
  when trt = 1, time = 0 vs when trt = 2 and time = 0;
PROC MIXED data=long;
  class s trt time ;
  model a = trt|time|b / solution ;
  repeated time / subject=s type=un ;
  estimate "estimate cov diffs" trt*b 1 -1 trt*time*b 1 0 -1 0 /e;
run;

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