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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1194 views
  • 2 likes
  • 3 in conversation