BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ccasagran737
Fluorite | Level 6

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?

  1. A macro variable DATE with the value 12SEP2008 is retrived from the local symbol table for the ONE macro
  2. A macro variable DATE with the value 12SEP2008 is retrived from the local symbol table for the TWO macro
  3. A macro variable DATE with the value 12SEP2008 is retrived from the global symbol table
  4. A macro variable DATE with the value 31DEC2006 is retrived from the global symbol table

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star
Defining the macros doesn't create any macro variables. The first statement here that actually executes is

%let date = 31DEC2006;

Since that appears outside of any macro definition, it creates &DATE in the global symbol table.

Next, the macros execute. Since &DATE already exists, neither macro creates a new variable. Both use the existing global &DATE.

View solution in original post

3 REPLIES 3
Astounding
PROC Star
Defining the macros doesn't create any macro variables. The first statement here that actually executes is

%let date = 31DEC2006;

Since that appears outside of any macro definition, it creates &DATE in the global symbol table.

Next, the macros execute. Since &DATE already exists, neither macro creates a new variable. Both use the existing global &DATE.
Tom
Super User Tom
Super User

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
Quentin
Super User

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).

The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 2094 views
  • 3 likes
  • 4 in conversation