BookmarkSubscribeRSS Feed
johns576
Calcite | Level 5
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
2 REPLIES 2
Cynthia_sas
SAS Super FREQ
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
chang_y_chung_hotmail_com
Obsidian | Level 7
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]

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 2 replies
  • 903 views
  • 0 likes
  • 3 in conversation