DATA Step, Macro, Functions and more

Calling macros conditionally

Reply
Contributor
Posts: 29

Calling macros conditionally

I try calling three macros from within a macro conditionally using call execute, however, the macros are resolved but the SAS statements dont get executed. This code behaves capriciously. Is there a way to call macros like this? Moreover, if i try breaking out from a do loop, can i come back in the same loop after the macro work is done  to execute further statements ?

%macro superset;

%if <condition> %then %do;

data _null_;

call execute(%subset1);

call execute(%subset2);

run;

%else do;

call execute(%subset3);

%end;

%macro subset1;

<SAS statements>

%mend subset1;

%macro subset2;

<SAS statements>

%mend subset2;

%macro subset3;

<SAS statements>

%mend subset3;

%mend superset;

Super User
Posts: 11,343

Re: Calling macros conditionally

Posted in reply to varunnakra

The argument string for Call Execute has to be in quotes: call execute('%subset1'); for example.

Contributor
Posts: 29

Re: Calling macros conditionally

Sorry I forgot to put quotes here..But i have used them in the original code..

Super User
Super User
Posts: 7,042

Re: Calling macros conditionally

Posted in reply to varunnakra

You do not need to use CALL EXECUTE to call a macro. Especially when already executing a macro.

Also it is better to not nest macro definitions.  It is just confusing to read and extra work for the SAS run time compiler to keep re-compiling the sub macros.

%macro subset1;

<SAS statements>

%mend subset1;

%macro subset2;

<SAS statements>

%mend subset2;

%macro subset3;

<SAS statements>

%mend subset3;


%macro superset;

  %if <condition> %then %do;

    %subset1;

    %subset2;

  %else do;

    %subset3;

  %end;

%mend superset;

%superset;

Contributor
Posts: 29

Re: Calling macros conditionally

Thanks Tom, however, if my sub macros need to have access to the local macro variables of the superset macro and I havent nested them then I will have to use parametric definitions and pass those variables as parameters..am i right?

Super User
Super User
Posts: 7,042

Re: Calling macros conditionally

Posted in reply to varunnakra

No.  The nesting of macro variable scope is related to execution and not where/when the macro was defined.

If supermacro creates variable X then calls submacro the value of X is available to submacro to read and to modify.

Contributor
Posts: 29

Re: Calling macros conditionally

Thanks, and it means that after calling the sub macro , may be in a loop, the control can return back to that loop and execute the SAS statements inside the loop and move forward?.. The control executes the sub macro and comes back , right?

Super User
Super User
Posts: 7,042

Re: Calling macros conditionally

Posted in reply to varunnakra

Right, but you need to remember what control we are talking about. The macro executes and basically produces text that SAS will interpret as statements to be compiled and run.  The macro could be something that generates many procs and data steps or it could generate just a few statements, or even part of a statement or a single word or token.  Or it could even only manipulate macros variables and not generate any code that SAS could execute.

Contributor
Posts: 22

Re: Calling macros conditionally

Posted in reply to varunnakra

Thanks to Tom to remember me taht the call execute will be executed after the datastep close. I fogot this.

Instead to use this I prefer use this

%macro condition_1;

     sas code ...

%mend;

%macro condition_2;

     sas code ...

%mend;

filename x temp lrecl=1024;

data _null_;

     file x;

     length flow $2000;

          if index("&flow",'FIRST CONDITION ') gt 0 then do;

          put '%condifiton_1;';

     End;

     else do;

          if index("&flow",'KPI_MO_REPORT') gt 0 then do;

               put '%condittion_2;';

          End;

          else do;

               put '%put call other macro programs if need ;';

          end;

     end;

run;

%inc x;;

filename x;

to debug it is more easy.

Ask a Question
Discussion stats
  • 8 replies
  • 1515 views
  • 2 likes
  • 4 in conversation