BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
uday
Calcite | Level 5

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;

1 ACCEPTED SOLUTION

Accepted Solutions
AncaTilea
Pyrite | Level 9

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

4 REPLIES 4
AncaTilea
Pyrite | Level 9

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

ballardw
Super User

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

uday
Calcite | Level 5

Thank you so much!

PaigeMiller
Diamond | Level 26

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;

--
Paige Miller

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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