%STR and %NRSTR Functions

Mask special characters and mnemonic operators in constant text at macro compilation.
Type: Macro quoting function
See: %NRQUOTE Function

Syntax

%STR (character-string)
%NRSTR (character-string)

Details

The %STR and %NRSTR functions mask a character string during compilation of a macro or macro language statement. They mask the following special characters and mnemonic operators:
+ − * / < > = ¬ ^ ~ ; ,  # blank
AND OR NOT EQ NE LE LT GE GT IN
They also mask the following characters when they occur in pairs and when they are not matched and are marked by a preceding %
' " ( )
In addition, %NRSTR also masks the following characters:
& %
Using %STR and %NSTR Arguments
Argument
Use
Percent sign before a quotation mark - for example, %' or %”,
Percent sign with quotation mark
EXAMPLE: %let percent=%str(Jim%'s office);
Percent sign before a parenthesis - for example, %( or %)
Two percent signs (%%):
EXAMPLE: %let x=%str(20%%);
Character string with the comment symbols /* or -->
%STR with each character
EXAMPLE: %str(/) %str(*) comment-text %str(*)%str(/)
%STR is most useful for character strings that contain
  • a semicolon that should be treated as text rather than as part of a macro program statement
  • blanks that are significant
  • a quotation mark or parenthesis without a match
Putting the same argument within nested %STR and %QUOTE functions is redundant. This example shows an argument that is masked at macro compilation by the %STR function and so remains masked at macro execution. Thus, in this example, the %QUOTE function used here has no effect.
%quote(%str(argument))
CAUTION:
Do not use %STR to enclose other macro functions or macro invocations that have a list of parameter values.
Because %STR masks parentheses without a match, the macro processor does not recognize the arguments of a function or the parameter values of a macro invocation.
For a description of quoting in SAS macro language, see Macro Quoting.
Note: The maximum level of nesting for macro quoting functions is 10.

Comparisons

  • Of all the macro quoting functions, only %NRSTR and %STR take effect during compilation. The other macro quoting functions take effect when a macro executes.
  • %STR and %NRSTR mask the same items as %QUOTE and %NRQUOTE. However, %QUOTE and %NRQUOTE work during macro execution.
  • If resolution of a macro expression produces items that need to be masked, use the %BQUOTE or %NRBQUOTE function instead of the %STR or %NRSTR function.

Examples

Example 1: Maintaining Leading Blanks

This example enables the value of the macro variable TIME to contain leading blanks.
%let time=%str(   now);
%put Text followed by the value of time:&time;
When this example is executed, these lines are written to the SAS log:
Text followed by the value of time:   now

Example 2: Protecting a Blank So That It Will Be Compiled as Text

This example specifies that %QSCAN use a blank as the delimiter between words.
%macro words(string);
   %local count word;
   %let count=1;
   %let word=%qscan(&string,&count,%str( ));
   %do %while(&word ne);
      %let count=%eval(&count+1);
      %let word=%qscan(&string,&count,%str( ));
   %end;
   %let count=%eval(&count-1);
   %put The string contains &count words.;
%mend words;
%words(This is a very long string)
When this program executes, these lines are written to the SAS log:
The string contains 6 words.

Example 3: Quoting a Value That Might Contain a Macro Reference

The macro REVRS reverses the characters produced by the macro TEST. %NRSTR in the %PUT statement protects %test&test so that it is compiled as text and not interpreted.
%macro revrs(string);
   %local nstring;
   %do i=%length(&string) %to 1 %by -1;
      %let nstring=&nstring%qsubstr(&string,&i,1);
   %end;
 &nstring
%mend revrs;
%macro test;
   Two words
%mend test;
%put %nrstr(%test%test) - %revrs(%test%test);
When this program executes, the following lines are written to the SAS log:
1          %macro revrs(string);
2             %local nstring;
3             %do i=%length(&string) %to 1 %by -1;
4                %let nstring=&nstring%qsubstr(&string,&i,1);
5             %end;&nstring
6          %mend revrs;
7
8          %macro test;
9             Two words
10         %mend test;
11
12         %put %nrstr(%test%test) - %revrs(%test%test);
%test%test - sdrow owTsdrow owT
NOTE: SAS Institute Inc., SAS Campus Drive, Cary, NC USA 27513-2414
NOTE: The SAS System used:
      real time           0.28 seconds
      cpu time            0.12 seconds