t or T trims trailing blanks from the string and charlist arguments. If you
want to remove trailing blanks from only one character argument
instead of both character arguments, then use the TRIM function
instead of the %SCAN function with the T modifier.
u or U adds uppercase letters to the list of characters.
w or W adds printable (writable) characters to the list of characters.
x or X adds hexadecimal characters to the list of characters.
Tip
If the modifier argument is a character constant, then enclose it in quotation
marks. Specify multiple modifiers in a single set of quotation marks. A
modifier argument can also be expressed as a character variable or expression.
Details
The %SCAN and %QSCAN functions search argument and return the nth word. A word
is one or more characters separated by one or more delimiters.
%SCAN does not mask special characters or mnemonic operators in its result, even
when the argument was previously masked by a macro quoting function. %QSCAN
masks the following special characters and mnemonic operators in its result:
& % ' " ( ) + − * / < > = ¬ ^ ~ ; , # blank
AND OR NOT EQ NE LE LT GE GT IN
Definition of “Delimiter” and “Word”
A delimiter is any of several characters that are used to separate words. You can specify
the delimiters in the charlist and modifier arguments.
If you specify the Q modifier, then delimiters inside substrings that are enclosed in
quotation marks are ignored.
In the %SCAN function, “word” refers to a substring that has all of the following
characteristics:
is bounded on the left by a delimiter or the beginning of the string
is bounded on the right by a delimiter or the end of the string
contains no delimiters
A word can have a length of zero if there are delimiters at the beginning or end of the
string, or if the string contains two or more consecutive delimiters. However, the
%SCAN function ignores words that have a length of zero unless you specify the M
modifier.
Using Default Delimiters in ASCII and EBCDIC Environments
If you use the %SCAN function with only two arguments, then the default delimiters
depend on whether your computer uses ASCII or EBCDIC characters.
If your computer uses ASCII characters, then the default delimiters are as follows:
blank ! $ % & ( ) * + , - . / ; < ^¦
In ASCII environments that do not contain the ^ character, the %SCAN function uses
the ~ character instead.
If your computer uses EBCDIC characters, then the default delimiters are as follows:
blank ! $ % & ( ) * + , - . / ; < ¬ | ¢¦
270 Chapter 17 Macro Functions
If you use the modifier argument without specifying any characters as delimiters, then
the only delimiters that will be used are delimiters that are defined by the modifier
argument. In this case, the lists of default delimiters for ASCII and EBCDIC
environments are not used. In other words, modifiers add to the list of delimiters that are
specified by the charlist argument. Modifiers do not add to the list of default modifiers.
Using the %SCAN Function with the M Modifier
If you specify the M modifier, then the number of words in a string is defined as one
plus the number of delimiters in the string. However, if you specify the Q modifier,
delimiters that are inside quotation marks are ignored.
If you specify the M modifier, then the %SCAN function returns a word with a length of
zero if one of the following conditions is true:
The string begins with a delimiter and you request the first word.
The string ends with a delimiter and you request the last word.
The string contains two consecutive delimiters and you request the word that is
between the two delimiters.
Using the %SCAN Function without the M Modifier
If you do not specify the M modifier, then the number of words in a string is defined as
the number of maximal substrings of consecutive non-delimiters. However, if you
specify the Q modifier, delimiters that are inside quotation marks are ignored.
If you do not specify the M modifier, then the %SCAN function does the following:
ignores delimiters at the beginning or end of the string
treats two or more consecutive delimiters as if they were a single delimiter
If the string contains no characters other than delimiters, or if you specify a count that is
greater in absolute value than the number of words in the string, then the %SCAN
function returns one of the following:
a single blank when you call the %SCAN function from a DATA step
a string with a length of zero when you call the %SCAN function from the macro
processor
Using Null Arguments
The %SCAN function allows character arguments to be null. Null arguments are treated
as character strings with a length of zero. Numeric arguments cannot be null.
Comparisons
%QSCAN masks the same characters as the %NRBQUOTE function.
Example: Comparing the Actions of %SCAN and
%QSCAN
This example illustrates the actions of %SCAN and %QSCAN.
%macro a;
aaaaaa
%mend a;
%macro b;
bbbbbb
%mend b;
%macro c;
%SCAN and %QSCAN Functions 271
cccccc
%mend c;
%let x=%nrstr(%a*%b*%c);
%put X: &x;
%put The third word in X, with SCAN: %scan(&x,3,*);
%put The third word in X, with QSCAN: %qscan(&x,3,*);
The %PUT statement writes these lines to the log:
X: %a*%b*%c
The third word in X, with SCAN: cccccc
The third word in X, with QSCAN: %c
%STR and %NRSTR Functions
Mask special characters and mnemonic operators in constant text at macro compilation.
Type: Macro quoting function
See: “%NRQUOTE Function” on page 264
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:
& %
Table 17.1 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%%);
272 Chapter 17 Macro Functions
Argument Use
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 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” on page 82.
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.
%STR and %NRSTR Functions 273
%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
274 Chapter 17 Macro Functions
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset