Base SAS 9.4.
I am trying to thoroughly understand Macro Quoting (‘masking’) within the context of ‘SAS Processing’ (Input Buffer / Word Scanner / Macro Processor / Symbol Tables (in this case Global only) / Macro Catalog / Compiler)
SAS code examples provided are quite simple but I am trying to breakdown the SAS Process to determine exactly where "unquoting is done for you" as referenced below.
FYI: I really could not find a more abbreviated manner in which to breakdown the various parts of the SAS Process .... so thanks in advance.
/* EXAMPLE 1 */
%let mv1=data test; var='a'; run;
%put &=mv1;
%put _user_;
I understand why Example 1 would fail
Macro Processor
begins to 'execute' the %let statement
adds MV1 to Global Symbol Table
mv1 data test (<< no semi)
The ';' represents an end of statement
";var='a'; run;" is never added to the GST
ERROR 180-322: Statement is not valid or it is used out of proper order.
/* EXAMPLE 2 */
%let mv2=%str(data test; var='a'; run;);
%put _user_;
%put &=mv2;
I understand why we need to use masking as in Example 2
Macro Processor
begins to 'execute' the %let statement
adds MV2 (masked) to Global Symbol Table
mv2 data test; var='a'; run;
>> NOTE: %put _user_; 'shows us' those delta characters
/* create macro variable*/
%let mv3=%str(data test; var='a'; run;);
%put _user_;
%put &=mv3;
/* Example 3a)*/
%macro test;
%put _user_;
&mv3;
%mend test;
%test;
I might expect that Example 3a would fail (given that there is no explicit %unquote to ‘unmask’ mv3 (the masked string found in the Global Symbol Table)).
However, does Example 3a fall under this description (from a SAS blog)?
>>> There are three cases where the unquoting is done for you:
The %UNQUOTE function was used
The item leaves the word scanner and is passed to the DATA step compiler, SAS Macro Facility, or other parts of the SAS System.
The value is returned from the %SCAN, %SUBSTR, or %UPCASE function
And, thus Example 3b (with explicit %unquote) further below is just not necessary?
I have tried to breakdown Example 3a) below in the context of SAS Processing to understand precisely where 'unquoting is done for you'
NOTE: the masked string mv3=%str(data test; var='a'; run;); .. already exists in the Global Symbol Table
Input Buffer
%macro test;
&mv3;
%mend test;
%test;
Word Scanner
detects %macro key word
MP is triggered
Macro Processor
During macro compilation
creates entry in the Macro Catalog
pulls tokens from the Input Buffer to the Macro Catalog until %mend
> %IF etc. as macro instructions
> noncompiled items as text
Macro Catalog
%macro test;
&mv3;
%mend test;
Input Buffer
%test;
Word Scanner
detects % trigger
Macro Processor is triggered
Macro Processor
begins to execute the macro
recognizes non-compiled text
places &mv2 into the IB
Input Buffer
&mv3.;
Word Scanner
continues to tokenize from the IB
recognizes macro trigger '&'
triggers MP
Is it here that the 'item' (the masked string from the Global Symbol Table) leaves the word scanner and is passed to the SAS Macro Facility and therefore, ‘unquoting is done for you’?
Macro Processor
looks in Local (and then Global) Symbol Table and resolves &mv2
>>> mv3 is now clearly unmasked <<<
&mv3; >>> (data test; var='a'; run;)
places non-compiled text into the Input Buffer
recognizes %mend
macro test ceases execution
Input Buffer
data test; var='a'; run;
Word Scanner
continues to tokenize from the IB
recognizes DATA as step boundary
triggers the DATA step compiler
Compiler
data test; var='a'; run;
The compiled DATA step is executed
The DATA step compiler is cleared
Therefore, Example 3b (where %unquote is explicit) is just not needed?
/* Example 3b)*/
%macro test;
%put _user_;
%unquote(&mv3);
%mend test;
%test;
... View more