/* ------------------------------------------------------------------------------------ */
/* File Name            : EXAMPLE_1.SAS                                                 */
/* Date Updated         : 18MAY04                                                       */
/* Written by           : Eun-Joo Lee                                                   */
/*                        Department of Statistics                                      */
/*                        Kansas State University                                       */
/* ------------------------------------------------------------------------------------ */
   libname AMMI 'c:\Program Files\SAS Institute\SAS\V8\AMMI Macros';
   options sasmstore=AMMI mstored;

   data SORGHUM;
     do Temp = '50','60','70','80','90';
       do Humidity = '20','40','60','80';
          input Height @@;
          output;
       end;
     end;
   cards;
   12.3 19.6 25.7 30.4
   13.7 16.9 27.0 31.5
   17.8 20.0 26.3 35.9
   12.1 17.4 36.9 43.4
    6.9 18.8 35.0 53.0
   ;
   run;

   filename i1_plot   "C:\PDF plots\i1_plot.pdf";
   filename i2_plot   "C:\PDF plots\i2_plot.pdf";
   filename s_plot    "C:\PDF plots\s_plot.pdf";
   filename t_plot    "C:\PDF plots\t_plot.pdf";
   filename m_plot1   "C:\PDF plots\m_plot1.pdf";
   filename m_plot2   "C:\PDF plots\m_plot2.pdf";

/* ------------------------------------------------------------------------------------ */
/* Macro Name            %PREVIEWAMMI                                                   */
/* Submacro Used         %READYDATA, %XIC2BY2, %XINTPLOT, %XSCREEPLOT                   */
/* Positional Parameters -------------------------------------------------------------- */
/*   1: data             name of input dataset                                          */
/*   2: respon           name of response variable                                      */
/*   3: factor1          name of the first classification variable                      */
/*   4: factor2          name of the second classification variable                     */
/* Keyword Parameters    -------------------------------------------------------------- */
/*   1: ic=1             1: produces a table of all 2*2 interaction contrasts (default) */
/*                       0: suppresses this output                                      */
/*   2: int=1            1: produces Type I and Type II interaction plots (default)     */
/*                       0: suppresses this output                                      */
/*   3: scree=1          1: produces a scree plot (default)                             */
/*                       0: suppresses this output                                      */
/* ------------------------------------------------------------------------------------ */

   %PREVIEWAMMI(SORGHUM,Height,Humidity,Temp);

   %PREVIEWAMMI(SORGHUM,Height,Humidity,Temp,ic=1,int=1,scree=1);

/* ------------------------------------------------------------------------------------ */
/* Macro Name            %FITAMMIMODEL                                                  */
/* Submacro Used         %READYDATA, %XADDITIVE, %XAMMI, %XMANDEL1, %XMANDEL2, %XTUKEY  */
/* Positional Parameters -------------------------------------------------------------- */
/*   1: data             name of input datas                                            */
/*   2: respon           name of response variable                                      */
/*   3: factor1          name of the first classification variable                      */
/*   4: factor2          name of the second classification variable                     */
/* Keyword Parameters    -------------------------------------------------------------- */
/*   1: additive=1       1: fits an additive model (default)                            */
/*                       0: suppresses this output                                      */
/*   2: Tukey=1          1: fits Tukey's model (default)                                */
/*                       0: suppresses this output                                      */
/*   3: Mandel=12        fits Mandel's bundle-of-straight-lines model                   */
/*                       12: regressed on factor1 and factor2, respectively (default)   */
/*                        1: regressed on factor1                                       */
/*                        2: regressed on factor2                                       */
/*                        0: suppresses this output                                     */
/*   4: AMMI=1           fits an AMMI model with # of multiplicative interaction terms  */
/*                       (must be an interger)                                          */
/*                       1: # of MI terms in ammi model = 1 (default)                   */
/*                       2: # of MI terms in ammi model = 2                             */
/*                       ......                                                         */
/*                       if # exceeds the maximum possible, then                        */
/*                       # = MIN(#(factor1),#(factor2)) - 1                             */
/*                       0: suppresses this output                                      */
/*   5: dfm=1            assigns a method to get degrees of freedom for each of the MI  */
/*                       terms in the AMMI model                                        */
/*                       1: looks up the table, AMMI.EX_L (default)                     */
/*                          if searching values are not in AMMI.EX_L, then              */
/*                          dfm=2 is assigned to the macro                              */
/*                       2: runs a simulation without storing results                   */
/*                       3: runs a simulation and stores results into AMMI.EX_L         */
/*                       4: Gollob's method, (b+t-1-2m) for the m-th MI term            */
/* ------------------------------------------------------------------------------------ */

   %FITAMMIMODEL(SORGHUM,Height,Humidity,Temp);

   %FITAMMIMODEL(SORGHUM,Height,Humidity,Temp,additive=1,Tukey=1,Mandel=1,AMMI=1,dfm=1);

   %FITAMMIMODEL(SORGHUM,Height,Humidity,Temp,additive=0,Tukey=0,Mandel=0,AMMI=2);

   %FITAMMIMODEL(SORGHUM,Height,Humidity,Temp,additive=0,Tukey=0,Mandel=0,AMMI=2,dfm=4);

/* ------------------------------------------------------------------------------------ */
/* Macro Name            %IC2BY2T                                                       */
/* Submacro Used         %CASE1, %CASE2, %CLASSLEVELS, %READYDATA                       */
/* Positional Parameters -------------------------------------------------------------- */
/*   1: data             name of input dataset                                          */
/*   2: respon           name of response variable                                      */
/*   3: factor1          name of the first classification variable                      */
/*   4: factor2          name of the second classification variable                     */
/* Keyword Parameters    -------------------------------------------------------------- */
/*   1: sigma2=          estimate of error variance for testing interaction contrasts   */
/*   2: dferrer=         degrees of freedom associated with estimated error variance    */
/* ------------------------------------------------------------------------------------ */

   %IC2BY2T(SORGHUM,Height,Humidity,Temp,sigma2=5.007969,dferror=8);

/* ------------------------------------------------------------------------------------ */
/* Macro Name            %LSMTUKEY                                                      */
/* Submacro Used         %READYDATA                                                     */
/* Positional Parameters -------------------------------------------------------------- */
/*   1: data             name of input dataset                                          */
/*   2: respon           name of response variable                                      */
/*   3: factor1          name of the first classification variable                      */
/*   4: factor2          name of the second classification variable                     */
/* Keyword Parameters    -------------------------------------------------------------- */
/*   1: at=              location on the factor1 axis in a Type II interaction plot     */
/*                       where least squares means are to be calculated                 */
/* ------------------------------------------------------------------------------------ */

   %LSMTUKEY(SORGHUM,Height,Humidity,Temp,at=20);

/* ------------------------------------------------------------------------------------ */
/* Macro Name            %LSMANDEL                                                      */
/* Submacro Used         %READYDATA, %XLSMANDEL1, %XLSMANDEL2                           */
/* Positional Parameters -------------------------------------------------------------- */
/*   1: data             name of input dataset                                          */
/*   2: respon           name of response variable                                      */
/*   3: factor1          name of the first classification variable                      */
/*   4: factor2          name of the second classification variable                     */
/* Keyword Parameters    -------------------------------------------------------------- */
/*   1: Mandel=1         fits Mandel's bundle-of-straight-lines model                   */
/*                       1: regressed on factor1 (default)                              */
/*                       2: regressed on factor2                                        */
/*   2: at=0             location on the factor1(factor2) axis in a Type II interaction */
/*                       plot where least squares means are to be calculated            */
/* ------------------------------------------------------------------------------------ */

   %LSMANDEL(SORGHUM,Height,Humidity,Temp,Mandel=1,at=20);

/* ------------------------------------------------------------------------------------ */
/* Macro Name            %CONTRASTAMMI                                                  */
/* Submacro Used         %CONDITION1, %CONDITION2, %CONDITION3, %CONDITION4, %READYDATA */
/*                       %XAMMI                                                         */
/* Positional Parameters -------------------------------------------------------------- */
/*   1: data             name of input dataset                                          */
/*   2: respon           name of response variable                                      */
/*   3: factor1          name of the first classification variable                      */
/*   4: factor2          name of the second classification variable                     */
/* Keyword Parameters    -------------------------------------------------------------- */
/*   1: H=0              contrast matrix associated with alpha1 in AMMI(1) model        */
/*                       (must be assigned using braces ({}) to enclose the values and  */
/*                        commas to separate the rows in parentheses)                   */
/*                       otherwise H=0 is assigned to the macro                         */
/*                       ex. h=({1 -1 0 0, 1 0 -1 0})                                   */
/*   2: G=0              contrast matirx associated with gamma1 in AMMI(1) model        */
/*                       (must be assigned using braces ({}) to enclose the values and  */
/*                        commas to separate the rows in parentheses)                   */
/*                       otherwise G=0 is assigned to the macro                         */
/*                       ex. g=({1 -1 0 0 0, 1 0 -1 0 0})                               */
/*   3: sim=1            1: runs a simulation to get a p-value and 90% & 95%            */
/*                          critical points for the assigned LR test (default)          */
/*                       0: suppresses simulation                                       */
/* ------------------------------------------------------------------------------------ */

   %CONTRASTAMMI(SORGHUM,Height,Humidity,Temp,H=0,G=({1 -1 0 0 0, 1 0 -1 0 0}),sim=0);

   %CONTRASTAMMI(SORGHUM,Height,Humidity,Temp,H=0,G=({1 -1 0 0 0, 1 0 -1 0 0}));