Hi,
I am studying for SAS Advanced programmer certificate.
I can't solve this problem. This is the exam question:
%macro one (input);
%two;
%put the value is &date;
%mend;
%macro two;
data _null_;
call symput('date','12SEP2008');
run;
%mend;
%let date = 31DEC2006;
%one (&date);
What is the result when the %put statement executes?
The correct answer is the 3. But I can't understand why. I thought the corret anserw was 2 and the macro was stored in the local symble table beacause the code is inside of a macro defintion.
Can you help me? Thanks a lot,
Daniele
One reason that the answer cannot be 2 is because the macro TWO does not have ANY local macro variables.
You can demonstrate by pulling from the SASHELP.VMACRO metadata table.
%macro three;
%let local3_mvar=This is local to THREE;
data _null_;
set sashelp.vmacro ;
where scope not in ('AUTOMATIC' 'GLOBAL')
or name = 'DATE'
;
put (_all_) (=);
run;
%mend three;
%macro two;
data _null_;
call symput('date','12SEP2008');
run;
%three;
%mend;
%macro one (input);
%two;
%put the value is &date;
%mend;
%let date = 31DEC2006;
options mprint;
%one (&date);
MPRINT(TWO): data _null_; MPRINT(TWO): call symput('date','12SEP2008'); MPRINT(TWO): run; NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.01 seconds MPRINT(THREE): data _null_; MPRINT(THREE): set sashelp.vmacro ; MPRINT(THREE): where scope not in ('AUTOMATIC' 'GLOBAL') or name = 'DATE' ; MPRINT(THREE): put (_all_) (=); MPRINT(THREE): run; scope=THREE name=LOCAL3_MVAR offset=0 value=This is local to THREE scope=ONE name=INPUT offset=0 value=31DEC2006 scope=GLOBAL name=DATE offset=0 value=12SEP2008 NOTE: There were 3 observations read from the data set SASHELP.VMACRO. WHERE scope not in ('AUTOMATIC', 'GLOBAL') or (name='DATE'); NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.01 seconds MPRINT(TWO): ; MPRINT(ONE): ; the value is 12SEP2008
While you're learning about MACRO scope, you might test out what happens if you remove the %LET statement, i.e.:
%macro one (input);
%two;
%put the value is &date;
%mend;
%macro two;
data _null_;
call symput('date','12SEP2008');
run;
%put _user_ ;
%mend;
/*%let date = 31DEC2006;*/
%one (31DEC2006)
Which scope would you expect the macro variable DATE to be created in now? CALL SYMPUT has some interesting rules it uses when deciding where to write a macro variable, but I won't spoil the answer.
By the way, this is a good example of why life is much easier if you explicitly declare macro variables as %local to the macro where they are created, rather than rely on the hope that SAS will make them local for you. (Also the newer CALL SYMPUTX allows you to specify a scope).
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.