BookmarkSubscribeRSS Feed
varunnakra
Fluorite | Level 6

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;

8 REPLIES 8
ballardw
Super User

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

varunnakra
Fluorite | Level 6

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

Tom
Super User Tom
Super User

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;

varunnakra
Fluorite | Level 6

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?

Tom
Super User Tom
Super User

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.

varunnakra
Fluorite | Level 6

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?

Tom
Super User Tom
Super User

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.

lacrefa
Calcite | Level 5

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.

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
  • 8 replies
  • 9415 views
  • 3 likes
  • 4 in conversation