Suppose you want the name (not the value) of a macro
variable to be printed by the %PUT statement. To do so, you must use
the %NRSTR function to mask the & and prevent the resolution of
the macro variable, as in the following example:
%macro example;
%local myvar;
%let myvar=abc;
%put %nrstr(The string &myvar appears in log output,);
%put instead of the variable value.;
%mend example;
%example
This code writes the
following text to the SAS log:
The string &myvar appears in log output,
instead of the variable value.
If you did not use the
%NRSTR function or if you used %STR, the following undesired output
would appear in the SAS log:
The string abc appears in log output,
instead of the variable value.
The %NRSTR function
prevents the & from triggering macro variable resolution.
The %NRSTR function
is also useful when the macro definition contains patterns that the
macro processor would ordinarily recognize as macro variable references,
as in the following program:
%macro credits(d=%nrstr(Mary&Stacy&Joan Ltd.));
footnote "Designed by &d";
%mend credits;
Using %NRSTR causes
the macro processor to treat &STACY and &JOAN simply as part
of the text in the value of D; the macro processor does not issue
warning messages for unresolvable macro variable references. Suppose
you invoke the macro CREDITS with the default value of D, as follows:
Submitting this program
generates the following FOOTNOTE statement:
footnote "Designed by Mary&Stacy&Joan Ltd.";
If you omit the %NRSTR
function, the macro processor attempts to resolve the references &STACY
and &JOAN as part of the resolution of &D in the FOOTNOTE
statement. The macro processor issues these warning messages (assuming
the SERROR system option, described in
, is active)
because no such macro variables exist:
WARNING: Apparent symbolic reference STACY not resolved.
WARNING: Apparent symbolic reference JOAN not resolved.
Here is a final example
of using %NRSTR. Suppose you wanted to have a text string include
the name of a macro function:
This is the result of %NRSTR
. Here is the program:
%put This is the result of %nrstr(%nrstr);
You must use %NRSTR
to mask the % sign at compilation, so the macro processor does not
try to invoke %NRSTR a second time. If you did not use %NRSTR to mask
the string
%nrstr
, the macro processor would
complain about a missing open parenthesis for the function.