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

Hi everyone. 

 

I am calling a macro (which has been written by somebody else) inside a wrapper macro. The macro that is being called cannot be modified. The author of the called macro did not declare variables used within that macro using LOCAL scope. So, if I call this macro, it may modify variables in the wrapper macro. 

 

For example: 

 

%macro cannot_modify;

  %let index = 123;

%mend;

 

%macro wrapper;

  /* Intention is to run %cannot_modify 10 times, but it will only run once. */

  %do index = 1 %to 10;

    %cannot_modify;

  %end;

%mend;

 

What I'd like to know is: is there some way of protecting the variables in the wrapper code so that the called macro cannot modify them, without modifying the called macro? 

 

Hope I've explained this clearly.

 

Thanks very much. 

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

Hello,

 

You can create an intermediary macro that declares index as local :

 

%macro cannot_modify;
  %let index = 123;
%mend;

%macro can_modify;
    %local index;
    %cannot_modify;
%mend; 

%macro wrapper;

  %do index = 1 %to 10;
    %can_modify;

	%put &=index.;
  %end;

%mend;

%wrapper;

View solution in original post

3 REPLIES 3
gamotte
Rhodochrosite | Level 12

Hello,

 

You can create an intermediary macro that declares index as local :

 

%macro cannot_modify;
  %let index = 123;
%mend;

%macro can_modify;
    %local index;
    %cannot_modify;
%mend; 

%macro wrapper;

  %do index = 1 %to 10;
    %can_modify;

	%put &=index.;
  %end;

%mend;

%wrapper;
ben12
Fluorite | Level 6
Looks good - I think that might solve my problem. Thank you!
RW9
Diamond | Level 26 RW9
Diamond | Level 26

If you find you need to do something outside of locally scoped macro variables, which is not at a system level, then I would conclude that there is a far better method to solve your particular problem, and that proceedng down the route of globalising, or wrapping variables will just make your code messy and unmaintainable.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 703 views
  • 2 likes
  • 3 in conversation