DATA Step, Macro, Functions and more

Variable scoping issue when passing parameters to nested inner macro

Reply
New Contributor
Posts: 4

Variable scoping issue when passing parameters to nested inner macro

I have been trying to create some useful autocall macros for use in future programs and I have run into a bit of an implementation problem.

I am trying to create a macro that will take the first parameter to determine the type of variables to create and the second parameter to declare what values to assign the variables. Here is what I have so far with a month example

%macro ifvar(parm1, parm2);
%if %upcase(&parm1) = MON %then %do;
%if %upcase(&parm2)=JAN %then %do;
data _null_;
call symput('month', 'January');
call symput('mm','01');
run;
%end;

%if %upcase(&parm2)=FEB %then %do;
data _null_;
call symput('mm','02');
call symput('month','February');
run;
%end;
..........
%end;

%if %upcase(&parm1) = MM %then %do;
%if %upcase(&parm2)=01 %then %do;
data _null_;
call symput('month','January');
call symput('mon','jan');
run;
%end;
..............
%end;
..................
%mend ifvar;


The problem is that %ifvar creates parm1 and parm2 in its local table so the new variables all get created there and when I try to pass it to the outer table in a dummy macro it comes out empty. I am able to get them to pass to an outer table if I take out the parameters and simply declare them with a %local and %let statement in the outer macro but this seems to defeat the purpose of parameters.

Any suggestions for how to deal with this or advice on what I am doing wrong? I am trying to avoid having to declare a global variable list each time this macro is run to keep things tight but I am thinking that may be the only answer.
-Thanks
SAS Super FREQ
Posts: 8,864

Re: Variable scoping issue when passing parameters to nested inner macro

Hi:
You might want to read the section in the macro documentation on scoping issues. The %LOCAL and %GLOBAL statements exist for the purpose of allowing you to control which symbol table your macro variables are stored in.
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a001072111.htm
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a000206835.htm
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a000206954.htm

cynthia
Regular Contributor
Posts: 241

Re: Variable scoping issue when passing parameters to nested inner macro

This is nothing to do with either nesting of macros, passing parameters or symbol tables. This is so simple in macro that I am not even sure these deserve to be autocall'ed. Hope this helps.
[pre]
%macro mon2mm(mon);
%sysfunc(putn("01&mon.2000"d,mmyy5),2)
%mend mon2mm;
%macro mm2mon(mm);
%sysfunc(inputn(&mm./01/2000,mmddyy10),monname3)
%mend mm2mon;

%put %mon2mm(Jan) %mon2mm(Feb) %mon2mm(Mar) %mon2mm(Nov) %mon2mm(Dec);
%put %mm2mon(01) %mm2mon(2) %mm2mon(3) %mm2mon(11) %mm2mon(12);
%*-- on log
01 02 03 11 12
Jan Feb Mar Nov Dec
--*;
[/pre]
Ask a Question
Discussion stats
  • 2 replies
  • 130 views
  • 0 likes
  • 3 in conversation