Hi,
My Macro has 2 sections. At the end of section 1, I get a mean value for variable, say xyz. If this mean is less than 10, I want SAS skip section 2 to move on a new loop.
Can you please help me with that?
Thank you so much.
HHC
%macro comp;
%local i j;
%do i=1 %to 100;
%do j=1 %to 100;
*PART 1: A lot of code ;
proc means data=abc;
var xyz;
output out=check_value
mean=mean_xyz;
run;
IF mean_xyz<10 THEN SKIP THE PART2 Code below
*PART 2: A lot of code ;
%end;
%end;
%mend;
@hhchenfxI have added the DATA step suggested by @LinusH and a macro %IF.
%macro comp;
%local i j;
%do i=1 %to 100;
%do j=1 %to 100;
*PART 1: A lot of code ;
proc means data=abc;
var xyz;
output out=check_value
mean=mean_xyz;
run;
data _null_;
set check_value;
call symputx('MEAN_XYZ',mean_xyz);
run;
%IF %sysevalf(&mean_xyz>=10) %THEN %do;
*PART 2: A lot of code ;
%end;
%end;
%end;
%mend comp;
Notice that in the %IF the %SYSEVALF is used. This is important because the mean (&MEAN_XYZ) will probably not be an integer. Non-integers would cause an alphabetic comparison (10<9. = true). I also reversed the comparison logic so that i could use a %DO block. There is also a %GOTO (much like the DATA step GOTO), but i am not a fan of it.
You probably need the conditional logic from %if.
To be able to do so, you need the mean_xyz as a macro variable, something like:
data _null_;
set check_value;
call symputx('MEAN_XYZ',mean_xyz);
run;
@hhchenfxI have added the DATA step suggested by @LinusH and a macro %IF.
%macro comp;
%local i j;
%do i=1 %to 100;
%do j=1 %to 100;
*PART 1: A lot of code ;
proc means data=abc;
var xyz;
output out=check_value
mean=mean_xyz;
run;
data _null_;
set check_value;
call symputx('MEAN_XYZ',mean_xyz);
run;
%IF %sysevalf(&mean_xyz>=10) %THEN %do;
*PART 2: A lot of code ;
%end;
%end;
%end;
%mend comp;
Notice that in the %IF the %SYSEVALF is used. This is important because the mean (&MEAN_XYZ) will probably not be an integer. Non-integers would cause an alphabetic comparison (10<9. = true). I also reversed the comparison logic so that i could use a %DO block. There is also a %GOTO (much like the DATA step GOTO), but i am not a fan of it.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.