Previous Page | Next Page

Macro Functions

%STR and %NRSTR Functions



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

Syntax
Details
Comparisons
Examples
Example 1: Maintaining Leading Blanks
Example 2: Protecting a Blank So That It Will Be Compiled As Text
Example 3: Quoting a Value That Might Contain a Macro Reference

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:

& %

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

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.  [cautionend]

For a description of quoting in SAS macro language, see Macro Quoting.

Note:   The maximum level of nesting for macro quoting functions is 10.  [cautionend]


Comparisons


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

Previous Page | Next Page | Top of Page