Creating a neat and reader-friendly SAS ANOVA report

ANOVA test is a generalization of the t-test. This macro serves the same purpose as ttest macro and general a cleaning report. The macro supports multiple var or byvar parameters.

Example:

data example;
set SASHELP.applianc;
if MOD(cycle,2)=1 then Group="A";
else Group="B";
keep units_1-units_6 group;
run;

proc print data=example(obs=8);
run;
%INCLUDE "/folders/myshortcuts/SASfolder/Tools/MacroANOVA.sas";
%MacroANOVA(table=example,var=units_1 units_2 units_3 units_4 units_5 units_6,Byvar=Group);
Result:

Macro:


%INCLUDE "/folders/myshortcuts/SASfolder/Tools/NumToStr.sas" ;
%MACRO MacroANOVA_hidden(table=,var=,Byvar=,alpha=);

proc sql noprint;
        select 
        "if "||compress("&Byvar= '"||&Byvar||"'")|| " then "||compress("&Byvar='level"||n||"'")
        into:ChangeLevel separated by ';'
                from temp1;
        select  compress("MEANlevel"||n)
        into:MEANList separated by ' '
                from temp1;       
        select  compress("STDlevel"||n)
        into:STDList separated by ' '
                from temp1;
        select compress("level"||n)
        into:NewLevelList separated by ' '
                from temp1;            

quit;

data TempData;
set TempData;
&ChangeLevel;
run;

proc means data=TempData mean STDDEV noprint;
var &var; 
class &Byvar ;
OUTPUT out=summaryTable;
run;
data summaryTable;
set summaryTable;
where (_Stat_="STD" or _Stat_="MEAN") and &Byvar  is not missing;
drop _type_ _freq_;
run;



proc transpose data=summaryTable out=summaryTable ;
   id _STAT_ &Byvar  ;
run;





data summaryTable ;
set summaryTable ;
array x[*] &MEANList &STDList;
array y[*] $20. &NewLevelList;
do i=1 to dim(y);
y[i]=compress(strip(put(x[i],10.2)||"("||put(x[i+dim(y)],10.2)||")"));
end;
length var $40;
var=_NAME_;
drop _NAME_;
run;
data summaryTable;
set summaryTable;
keep &NewLevelList var;
run;

/*===========================*/
ods select none;
proc anova data=TempData;
class &Byvar;
model &var= &Byvar;
ods output ModelANOVA=ANOVAP;
run;
ods select all;
data ANOVAP;
set ANOVAP;
length var $40;
var=dependent;
drop dependent;
run;

data ANOVAP;
set ANOVAP;
keep var FValue ProbF;
run;

proc sort data=summaryTable out=summaryTable;
by var;
run;
proc sort data=ANOVAP out=ANOVAP;
by var;
run;

data summaryTableP;
merge summaryTable ANOVAP;
by var;
run;

data summaryTableP;
set summaryTableP;
significant=" ";
if ProbF<&alpha and not missing(ProbF) then significant="*";
run;

data summaryTableP;
retain var;
set summaryTableP;
run;


%MEND MacroANOVA_hidden;


*Treat the multiple var case;
%MACRO MultiANOVA(table=,var=,Byvar=,alpha=,title=0);

%Num2Str(table=&table,var=&Byvar,newTable=TempData);

proc sort data=TempData out=temp1 nodupkey;
by &Byvar;
run;

data temp1;
set temp1;
length n $10.;
n=put(_n_,10.);
where not missing(&Byvar);
run;

proc sql noprint;
        select  "label "||compress("level"||n)||"="||'"'||&Byvar||'(MEAN& SD)"'
        into:LevelNote separated by ';'
                from temp1;           
quit;


%let aryLength_MultiANOVA=%sysfunc(COUNTW(&var,' '));
%MacroANOVA_hidden(table=&table,var=%sysfunc(SCAN(&var,1,' ')),Byvar=&byvar,alpha=&alpha);

data ANOVATableP;
set summaryTableP;
run;

%Do varIndex_multi=2 %To &aryLength_MultiANOVA;
%MacroANOVA_hidden(table=&table,var=%sysfunc(SCAN(&var,&varIndex_multi,' ')),Byvar=&byvar,alpha=&alpha);
data ANOVATableP;
set ANOVATableP summaryTableP;
run;
%end;

/*===========Print Table=============*/
%if &title=0 %then %do;
title10 "One-way ANOVA test for &Byvar versus continuous variables";
footnote10"The test is performed under the equal variance assumption";
/*footnote10"*P-value is given by the one-way ANOVA test";*/

proc print data=ANOVATableP noobs label;
label var='var';
&LevelNote;
label 
ProbF='P-value'
significant="Significance";
run;
footnote;
title0;
%end;
%MEND MultiANOVA;



%MACRO MacroANOVA(table=,var=,Byvar=,alpha=0.05);



%let aryLength_ANOVA=%sysfunc(COUNTW(&Byvar,' '));
%if &aryLength_ANOVA=1 %then %MultiANOVA(table=&table,var=&var,Byvar=&Byvar,alpha=&alpha);
%else %do;
/*If has multiple Byvar*/


%MultiANOVA(table=&table,var=&var,Byvar=%sysfunc(SCAN(&Byvar,1,' ')),title=2,alpha=&alpha);
data ANOVATableP;
set ANOVATableP;
var="%sysfunc(SCAN(&Byvar,1,' '))";
keep var Fvalue ProbF significant;
run;
data ANOVATableP1;
set ANOVATableP;
run;



%Do varIndex=2 %To &aryLength_ANOVA;
%MultiANOVA(table=&table,var=&var,Byvar=%sysfunc(SCAN(&Byvar,&varIndex,' ')),title=2,alpha=&alpha);
data ANOVATableP;
set ANOVATableP;
var="%sysfunc(SCAN(&Byvar,&varIndex,' '))";
keep var Fvalue ProbF significant;
run;
data ANOVATableP1;
set ANOVATableP1 ANOVATableP;
run;
%End;

title10 "One-way ANOVA tests for &var versus discrete variables";
footnote10 "The test is performed under an equal variance assumption";

proc print data=ANOVATableP1 label;
label 
var="Var"
ProbF='P-value'
significant="Significance";
run;
footnote;
title0;
/*===========Print Table=============*/
%end;





%MEND MacroANOVA;

Comments

Popular posts from this blog

Creating a neat and reader-friendly SAS t-test report