Hey Everyone,
I am trying to create a macro variable where I perform a One-Way ANOVA. If the pvalue for the test of equal variance is less than 0.05 then do a welch anova. Note: I have not included the "else" part of the statement as yet.
Here is my code:
%macro group3(dsn,class,outcome);
%let largestpvalue=;
%if &largestpvalue le 0.05 %then %do;
%let i=1;
%let alloutcome=%scan(&outcome,&i);
%let myoutcome=;
%do %until (&alloutcome eq );
%put &largestpvalue;
%let myoutcome=&myoutcome &alloutcome;
title "&alloutcome.";
proc glm data=&dsn;
class &class;
model &alloutcome=&class;
ods output HovFTest=&alloutcome.;
means &class/hovtest=levene(type=abs) welch;
lsmeans &class;
run;
proc sql;
select round(max(probF),.0001) as largestpvalue
into:largestpvalue
from &alloutcome.;
quit;title;
%let i=%eval(&i+1);
%let alloutcome=%scan(&outcome,&i);
%put &myoutcome;
%end;%end;
%mend group3;
%group3(sashelp.iris,species,Sepallength sepalwidth Petallength petalwidth);
What are you asking for help with?
I am asking for help with getting the correct result. The code is not giving me what I want. I want to do Welch ANOVA only when the pvalue is less than 0.05 (The pvalue for the test of equal variance).
How will we recognize a "correct result"?
We don't have your result.
"The code is not giving me what I want." is not a very clear description of what you do want.
Pvalue less than 0.05 for which exact test? The code you are showing appears to be attempting to run multiple ANOVA with different result variables. So I am not sure which specific Pvalue you are intending.
I suggest that you set OPTIONS MPRINT; and then run your macro and read the log as to the generated statements.
I strongly suspect that you have a timing issue of where in your code you compare the "largestpvalue" to when it is assigned.
Also there can be some issues with comparisons to values like 0.05 if you do not take care to use something like %sysevalf to make sure that a decimal comparison is performed and not a text comparison.
Post code into a text box or code box, opened with the </> and "running man" icons respectively to maintain code indents and such. Your code is a bit hard to follow without them and the forum software will reformat text not pasted into one of those boxes.
As an aside when "looping" over a list I find an explicit loop like:
%do i = 1 %to %sysfunc(countw(&list));
%let value = %scan(&list,&i);
a bit easier to follow and get the timing correct than a %do %until loop as you show for the "outcomes".
If you code:
%if &largestpvalue le 0.05 %then %do;
the macro language will use call %EVAL() to evaluate the expression, and because %EVAL does not know about decimal points it will do a text comparison. Since you want to do a numeric comparison, one improvement would be to explicitly call %SYSEVALF:
%if %sysevalf(&largestpvalue le 0.05) %then %do;
But there may be other issues as well.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.