Deciding How Much Text to Mask with a Macro
Quoting Function
In each of the following statements, the macro processor treats the masked semicolons as
text:
%let p=%str(proc print; run;);
%let p=proc %str(print;) %str(run;);
%let p=proc print%str(;) run%str(;);
The value of P is the same in each case:
proc print; run;
The results of the three %LET statements are the same because when you mask text with
a macro quoting function, the macro processor quotes only the items that the function
recognizes. Other text enclosed in the function remains unchanged. Therefore, the third
%LET statement is the minimalist approach to macro quoting. However, masking large
blocks of text with a macro quoting function is harmless and actually results in code that
is much easier to read (such as the first %LET statement).
%SUPERQ Function
Using %SUPERQ
The %SUPERQ function locates the macro variable named in its argument and quotes
the value of that macro variable without permitting any resolution to occur. It masks all
items that might require macro quoting at macro execution. Because %SUPERQ does
not attempt any resolution of its argument, the macro processor does not issue any
warning messages that a macro variable reference or a macro invocation has not been
resolved. Therefore, even when the %NRBQUOTE function enables the program to
work correctly, you can use the %SUPERQ function to eliminate unwanted warning
messages from the SAS log. %SUPERQ takes as its argument either a macro variable
name without an ampersand or a text expression that yields a macro variable name.
%SUPERQ retrieves the value of a macro variable from the macro symbol table and
quotes it immediately, preventing the macro processor from making any attempt to
resolve anything that might occur in the resolved value. For example, if the macro
variable CORPNAME resolves to Smith&Jones, using %SUPERQ prevents the macro
processor from attempting to further resolve
&Jones. This %LET statement
successfully assigns the value Smith&Jones to TESTVAR:
%let testvar=%superq(corpname);
Examples Using %SUPERQ
This example shows how the %SUPERQ function affects two macro invocations, one
for a macro that has been defined and one for an undefined macro:
%window ask
#5 @5 'Enter two values:'
#5 @24 val 15 attr=underline;
%SUPERQ Function 93
%macro a;
%put *** This is a. ***;
%mend a;
%macro test;
%display ask;
%put *** %superq(val) ***; /* Note absence of ampersand */
%mend test;
Suppose you invoke the macro TEST and respond to the prompt as shown:
%test
Enter the following: %a %x
The %PUT statement simply writes the following line:
*** %a %x ***
It does not invoke the macro A, and it does not issue a warning message that %X was
not resolved. The following two examples compare the %SUPERQ function with other
macro quoting functions.
Using the %SUPERQ Function to Prevent Warning Messages
The sections about the %NRBQUOTE function show that it causes the macro processor
to attempt to resolve the patterns &name and %name the first time it encounters them
during macro execution. If the macro processor cannot resolve them, it quotes the
ampersand or percent sign so that later uses of the value do not cause the macro
processor to recognize them. However, if the MERROR or SERROR option is in effect,
the macro processor issues a warning message that the reference or invocation was not
resolved.
The macro FIRMS3, shown here, shows how the %SUPERQ function can prevent
unwanted warning messages:
%window ask
#5 @5 'Enter the name of the company:'
#5 @37 val 50 attr=underline;
%macro firms3;
%global code;
%display ask;
%let name=%superq(val);
%if &name ne %then %let code=valid;
%else %let code=invalid;
%put *** &name is &code ***;
%mend firms3;
%firms3
Suppose you invoke the macro FIRMS3 twice and respond with the following
companies:
A&A Autos
Santos&D'Amato
After the macro executes, the following is written to the SAS log:
94 Chapter 7 Macro Quoting
*** A&A Autos is valid ***
*** Santos&D'Amato is valid ***
Using the %SUPERQ Function to Enter Macro Keywords
Suppose you create an online training system in which users can enter problems and
questions that another macro prints for you later. The user's response to a %WINDOW
statement is assigned to a local macro variable and then to a global macro variable.
Because the user is asking questions about macros, he or she might enter all sorts of
macro variable references and macro calls as examples of problems, as well as
unmatched, unmarked quotation marks and parentheses. If you mask the response with
%BQUOTE, you have used a few %PUT statements to warn the user about responses
that cause problems. If you use the %SUPERQ function, you need fewer instructions.
The macros ASK1 and ASK2 show how the macro code becomes simpler as you change
macro quoting functions.
The macro ASK1, below, shows how the macro looks when you use the %BQUOTE
function:
%window ask
#5 @5 'Describe the problem.'
#6 @5 'Do not use macro language keywords, macro calls,'
#7 @5 'or macro variable references.'
#9 @5 'Enter /// when you are finished.'
#11 @5 val 100 attr=underline;
%macro ask1;
%global myprob;
%local temp;
%do %until(%bquote(&val) eq %str(///));
%display ask;
%let temp=&temp %bquote(&val);
%end;
%let myprob=&temp
%mend ask1;
The macro ASK1 does not include a warning about unmatched quotation marks and
parentheses. You can invoke the macro ASK1 and enter a problem:
%ask1
Try entering:
Why did my macro not run when I called it? (It had three
parameters, but I wasn't using any of them.)
It ran after I submitted the next statement.
///
Notice that both the first and second lines of the response contain an unmatched,
unmarked quotation mark and parenthesis. %BQUOTE can handle these characters
during execution.
The macro ASK2, shown here, modifies the macro ASK1 by using the %SUPERQ
function. Now the %WINDOW statement accepts macro language keywords and does
not attempt to resolve macro calls and macro variable references:
%window ask
#5 @5 'Describe the problem.'
%SUPERQ Function 95
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset