Macro Functions |
Type: | Macro quoting function |
See also: | %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:
& %
%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))
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 |
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
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.
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
Copyright © 2009 by SAS Institute Inc., Cary, NC, USA. All rights reserved.