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.
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;
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;
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.