Passing Macro Variables

From sasCommunity
Jump to: navigation, search

Q: How can I pass macro variable values from one macro to another. Both are called from top level; this is not a routine calling a subroutine and user does not want to %global the values.


  1. in first macro save the local macro variables into a data set using sql dictionary.macros;
  2. in second macro use call execute to 'reconstitute' the macro variables.

Save in Data Set

options mprint;
%let a = b;
%macro one(data=sashelp.class
          ,testing=1);
%put &sysmacroname &=data;
proc sql; create table macro_variables as
          select scope, name, value
            from dictionary.macros
           where scope eq "&sysmacroname"
           and not (name like 'SQL%');
           quit;
%if &testing %then %do;
    proc print;title3 &syslast;
    %end;
run;
%mend one;
%one()
 
%symdel a;
 
%macro two(testing=1);
 
DATA _null_;
     attrib statement length = $128;
do until(endofile);
   set macro_variables end = endofile;
   putlog name= value=;
   statement = catx(' ','%let',name,'=',value,';');
   putlog statement=;
   call execute(statement);
   end;
stop;
run;
%if &testing %then %put _local_;
run;
 
%mend;
%put info: global m-vars;;
%put _user_;
 
%two()
 
%put _global_;


Commentary

  • not a good idea; this trick is not intuitive
  • the second macro receives all the macro variables of the first macro call and there is no way of knowing what is in the data set
  • better to allocate global macro variables and pass the values into each macro; e.g.;
%let in_data = sashelp.class;
 
%one(data=&in_data)
 
%two(data=&in_data)

Save in Macro Variable Sysparm

options sysparm = 'a=b,c=def,data=sashelp.class';

Unpack with Parse Sysparm

References

--Ronald_J._Fehd macro.maven == the radical programmer (talk) 20:11, 26 May 2015 (CDT)