DATA Step, Macro, Functions and more

sample macro

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

sample macro

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;


Accepted Solutions
Solution
‎01-25-2013 10:23 AM
Super Contributor
Posts: 543

Re: sample macro

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(&macro_var.);

          if a > 3;

     run;

*/

data DS2;

set DS1;

a = sqrt(b);

if (a > 3) then..../*then what?*/

%mend;

Smiley Happy

View solution in original post


All Replies
Solution
‎01-25-2013 10:23 AM
Super Contributor
Posts: 543

Re: sample macro

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(&macro_var.);

          if a > 3;

     run;

*/

data DS2;

set DS1;

a = sqrt(b);

if (a > 3) then..../*then what?*/

%mend;

Smiley Happy

Super User
Posts: 11,343

Re: sample macro

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).

New Contributor
Posts: 4

Re: sample macro

Thank you so much!

Trusted Advisor
Posts: 1,918

Re: sample macro

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(Smiley Wink;

%else a=b%str(Smiley Wink;

%mend;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 487 views
  • 4 likes
  • 4 in conversation