Learning SAS? Welcome to the exclusive online community for all SAS learners.

How to execute Macro conditionally.

Reply
Occasional Contributor
Posts: 17

How to execute Macro conditionally.

I am new to SAS. While creating macros, I got stuck at how to call a macro conditionally.

I created a Macro for printing the dataset Blood. Now I need to call this macro only when Chol variable is greater than 200.

Please suggest how should I proceed in this case.

Trusted Advisor
Posts: 3,214

Re: How to execute Macro conditionally.

Posted in reply to gurpreetkaur

please go back to SAS macro-concepts.

In some languages like Excel a macro is smething like keybord recording.

In SAS the macro language is text processing of the SAS-source (code) to be processed by the interpreter (compile and go in one).

It makes no sense to ask for macros being executed conditionally.

Macros can be used for manipulating SAS source (code) conditionally.

SAS source(code) can be code in a way to treat the data conditionally (that is what programming is).

---->-- ja karman --<-----
Super User
Super User
Posts: 7,050

Re: How to execute Macro conditionally.

Posted in reply to gurpreetkaur

Conditional code generation is what a macro is good at.

You could either make the macro smart enough to determine if CHOL > 200 and conditionally perform the print.

Or you could add a parameter to the macro and pass in the condition.

%macro print_blood (dsname,print=yes);

%if print=yes %then %do;

proc print data=&dsname ....

....

%end;

%mend ;

%let print=no ;

proc sql noprint ;

   select 'yes' into Smiley Tonguerint from MYDATA where chol > 200 ;

quit;

%print_blood(MYDATA,print=&print);

Ask a Question
Discussion stats
  • 2 replies
  • 314 views
  • 0 likes
  • 3 in conversation