Chapter Contents

Previous

Next
%STR and %NRSTR

%STR and %NRSTR



Mask special characters and mnemonic operators in constant text at macro compilation

Type: Macro quoting function
See also: %NRQUOTE


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 May 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

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

& %

When an argument contains... 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 Chapter 7 in SAS Macro Language: Reference.


Comparisons


Examples

Example 1: Maintaining Leading Blanks

This example allows the value of the macro variable TIME to contain leading blanks.

%let time=%str(   now);

%put Text followed by the value of time:&time;

Executing this example writes these lines 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)

Executing this program writes these lines to the SAS log:

The string contains 6 words.

Example 3: Quoting a Value That May 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);

Executing this program writes these lines to the SAS log:

%test&test - tset&sdrow owT


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.