As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.


Here are some tips for How to share your SAS knowledge with your professional network.


Difference between revisions of "Passing Macro Variables"

From sasCommunity
Jump to: navigation, search
(created)
 
(added sections, and code for sysparm)
Line 6: Line 6:
 
# in second macro use call execute to 'reconstitute' the macro variables.
 
# in second macro use call execute to 'reconstitute' the macro variables.
  
 +
==Save in Data Set==
 
<source lang="sas">
 
<source lang="sas">
 
options mprint;
 
options mprint;
Line 52: Line 53:
 
</source>
 
</source>
  
commentary:
+
 
 +
 
 +
==Commentary==
 
* not a good idea; this trick is not intuitive
 
* 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  
 
* 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  
Line 63: Line 66:
  
 
%two(data=&in_data)
 
%two(data=&in_data)
 +
</source>
  
 +
==Save in Macro Variable Sysparm==
 +
 +
<source lang="sas">
 +
options sysparm = 'a=b,c=def,data=sashelp.class';
 
</source>
 
</source>
  
 +
Unpack with [http://www.sascommunity.org/wiki/Parse_sysparm Parse Sysparm]
  
  

Revision as of 05:56, 27 May 2015

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


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