I am new to macro. Is the macro below valid?
%macro SASrules;
else
run;
data DS2;
set DS1;
a = sqrt(b);
if (a > 3) then
%mend;
No...
%macro SASrules;
else /*this needs a preceding IF statement, you can't have an ELSE just hanging around*/
run; /* this is out of place...."ELSE;RUN;....should be more like ELSE; <statement to be performed>; RUN;*/
/*this could be ok, but you need to have a MACRO variable...maybe the 'b', or the '3'....*/
/* something like
data DS2;
set DS1;
a = sqrt(¯o_var.);
if a > 3;
run;
*/
data DS2;
set DS1;
a = sqrt(b);
if (a > 3) then..../*then what?*/
%mend;
No...
%macro SASrules;
else /*this needs a preceding IF statement, you can't have an ELSE just hanging around*/
run; /* this is out of place...."ELSE;RUN;....should be more like ELSE; <statement to be performed>; RUN;*/
/*this could be ok, but you need to have a MACRO variable...maybe the 'b', or the '3'....*/
/* something like
data DS2;
set DS1;
a = sqrt(¯o_var.);
if a > 3;
run;
*/
data DS2;
set DS1;
a = sqrt(b);
if (a > 3) then..../*then what?*/
%mend;
The important thing to consider when using SAS macros is that they are program code generators. Your example would generate the text you provide into a program calling the macro. However as Anca tilea mentioned the place in your code where you call should have an IF statement, within a data step, before the Else or the generated code will cause errors. Also your Else statment is not properly ended (Run is not an executeable statement).
Thank you so much!
In addtion to the comments above, I wonder why you have created this macro at all. It contains no macro statements, so you could simply place the interior of the macro into the appropriate part of the larger program. So even if the code were correct, the macro serves no purpose whatsoever. It is a waste of time to turn this particular code into a macro.
Normally, when you want to write a macro, you have a situation where you will want the macro output to vary given certain conditions, and/or certain input values. For example, here is a small macro that checks to see if a SAS data set exists and performs certain actions depending on whether or not the SAS data set exists.
%macro check;
%if %sysfunc(exist(work.mydata)) %then a=sqrt(b)%str(;);
%else a=b%str(;);
%mend;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.