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 of 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
-
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 ! $ % & (
) * + , - . / ; < ¬ | ¢¦
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
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.