Search code examples
statisticssasmixed-modelsprocdo-loops

SAS do loop for testing covariance structers in PROC MIXED and saving/appending outputs


I am doing repeated measure analysis in SAS and I am comparing the differences in the covariance structure information criteria. Rather than just doing each individual analysis I am looking for a way to loop through them and save the outputs to a table.

Here is a sample of the data;

Data dta;
input A Subject B Y;
datalines;
1 1 1 3
1 1 2 4
1 1 3 7
1 1 4 7
1 2 1 6
1 2 2 5
1 2 3 8
1 2 4 8
2 3 1 1
2 3 2 2
2 3 3 5
2 3 4 10
2 4 1 2
2 4 2 3
2 4 3 6
2 4 4 10
;
run;

ods output InfoCrit = Output;
proc mixed data=dta ic covtest ;
 class A Subject B;
 model Y = A B A*B/DDFM=BW S;
 Repeated B / Subject=subject Type=CS;
run;

I would like to loop through the Type= in the repeated measurement part with AR(1), ARH(1), CS, CSH, HF, TOEP, and UNR. Below is the output from Type=CS.

Neg2LogLike     Parms   AIC     AICC    HQIC    BIC     CAIC
23.3                2   27.3    29.7    24.6    26.1    28.1

I would also like to add a new column for Type for each iteration of the do-loop;

Type    Neg2LogLike     Parms   AIC     AICC    HQIC    BIC     CAIC
CS           23.3           2   27.3    29.7    24.6    26.1    28.1
AR(1)        22.9           2   26.9    29.3    24.2    25.7    27.7

So far I have been able to use ods output to save the table and I have been able to manually change the Type= and append that output to a new table but I have been unsuccessful in implement the do loop to automate it.


Solution

  • You can create a macro that uses a space-delimited list and loop through it.

    %macro outputTypes(types=);
    
        %do i = 1 %to %sysfunc(countw(&types., %str( ) ));
            %let this_type = %sysfunc(scan(&types., &i., %str( ) ));
    
             ods output InfoCrit = _output_;
    
             proc mixed data=dta ic covtest ;
                 class A Subject B;
                 model Y = A B A*B/DDFM=BW S;
                 Repeated B / Subject=subject Type=&this_type.;
             run;
    
             data output_&i.;
                 length type $10.;
                 set _output_;
                 type = "&this_type.";
             run;
        %end;
    
        data all_types;
            set output_:;
        run;
    
        /* Remove temp data */
        proc datasets lib=work nolist;
            delete output_:
                   _output_
            ;
        quit;
    
    %mend;
    

    Simply specify the types you'd like and they'll automatically be put into proc mixed:

    %outputTypes(types=AR(1) ARH(1) CS CSH HF TOEP UNR);
    

    Output:

    type    Neg2LogLike Parms   AIC         AICC    HQIC    BIC     CAIC
    AR(1)   22.9        2       26.9        29.3    24.2    25.7    27.7
    ARH(1)  15.9        5       25.9        55.9    19.2    22.8    27.8
    CS      23.3        2       27.3        29.7    24.6    26.1    28.1
    CSH     15.3        5       25.3        55.3    18.6    22.2    27.2
    HF      15.3        5       25.3        55.3    18.6    22.2    27.2
    TOEP    22.8        4       30.8        44.2    25.4    28.4    32.4
    UNR     22.8        4       30.8        44.2    25.4    28.4    32.4
    

    Use ods select none to prevent writing any actual ODS graphs but still allow the ods output statement to work. Use ods select all to re-enable graphs.