Returns the position and length of the nth word from a character string.
Category: | Character |
Interaction: | When invoked by the %SYSCALL macro statement, CALL SCAN removes the quotation marks from its arguments. For more information, see Using CALL Routines and the %SYSCALL Macro Statement . |
is a nonzero numeric constant, variable, or expression that has an integer value that specifies the number of the word in the character string that you want the CALL SCAN routine to select. For example, a value of 1 indicates the first word, a value of 2 indicates the second word, and so on. The following rules apply:
specifies a numeric variable in which the position of the word is returned. If count exceeds the number of words in the string, then the value that is returned in position is zero. If count is zero or missing, then the value that is returned in position is missing.
specifies a numeric variable in which the length of the word is returned. If count exceeds the number of words in the string, then the value that is returned in length is zero. If count is zero or missing, then the value that is returned in length is missing.
specifies a character constant, variable, or expression.
specifies an optional character constant, variable, or expression that initializes a list of characters. This list determines which characters are used as the delimiters that separate words. The following rules apply:
Tip | You can add more characters to charlist by using other modifiers. |
specifies a character constant, variable, or expression in which each non-blank character modifies the action of the CALL SCAN routine. Blanks are ignored. You can use the following characters as modifiers:
a or A | adds alphabetic characters to the list of characters. |
b or B | scans backwards, from right to left instead of from left to right, regardless of the sign of the count argument. |
c or C | adds control characters to the list of characters. |
d or D | adds digits to the list of characters. |
f or F | adds an underscore and English letters (that is, valid first characters in a SAS variable name using VALIDVARNAME=V7) to the list of characters. |
g or G | adds graphic characters to the list of characters. Graphic characters are those that, when printed, produce an image on paper. |
h or H | adds a horizontal tab to the list of characters. |
i or I | ignores the case of the characters. |
k or K | causes all characters that are not in the list of characters to be treated as delimiters. That is, if K is specified, then characters that are in the list of characters are kept in the returned value rather than being omitted because they are delimiters. If K is not specified, then all characters that are in the list of characters are treated as delimiters. |
l or L | adds lower case letters to the list of characters. |
m or M | specifies that multiple consecutive delimiters, and delimiters at the beginning or end of the string argument, refer to words that have a length of zero. If the M modifier is not specified, then multiple consecutive delimiters are treated as one delimiter, and delimiters at the beginning or end of the string argument are ignored. |
n or N | adds digits, an underscore, and English letters (that is, the characters that can appear in a SAS variable name using VALIDVARNAME=V7) to the list of characters. |
o or O | processes the charlist and modifier arguments only once, rather than every time the CALL SCAN routine is called. Using the O modifier in the DATA step can make CALL SCAN run faster when you call it in a loop where the charlist and modifier arguments do not change. The O modifier applies separately to each instance of the CALL SCAN routine in your SAS code, and does not cause all instances of the CALL SCAN routine to use the same delimiters and modifiers. |
p or P | adds punctuation marks to the list of characters. |
q or Q | ignores delimiters that are inside of substrings that are enclosed in quotation marks. If the value of the string argument contains unmatched quotation marks, then scanning from left to right will produce different words than scanning from right to left. |
s or S | adds space characters to the list of characters (blank, horizontal tab, vertical tab, carriage return, line feed, and form feed). |
t or T | trims trailing blanks from the string and charlist arguments. If you want to remove trailing blanks from just one character argument instead of both character arguments, then use the TRIM function instead of the CALL SCAN routine with the T modifier. |
u or U | adds upper case 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. |
data firstlast; input String $60.; call scan(string, 1, First_Pos, First_Length); First_Word = substrn(string, First_Pos, First_Length); call scan(string, –1, Last_Pos, Last_Length); Last_Word = substrn(string, Last_Pos, Last_Length); datalines4; Jack and Jill & Bob & Carol & Ted & Alice & Leonardo ! $ % & ( ) * + , - . / ; ;;;; proc print data=firstlast; var First: Last:; run;
data all; length word $20; drop string; string = ' The quick brown fox jumps over the lazy dog. '; do until(position <= 0); count+1; call scan(string, count, position, length); word = substrn(string, position, length); output; end; run; proc print data=all noobs; var count position length word; run;
data comma; length word $30; string = ',leading, trailing,and multiple,,delimiters,,'; do until(position <= 0); count + 1; call scan(string, count, position, length, ',', 'mo'); word = substrn(string, position, length); output; end; run; proc print data=comma noobs; var count position length word; run;
data test; length word word_r $30; string = 'He said, "She said, ""No!""", not "Yes!"'; do until(position <= 0); count + 1; call scan(string, count, position, length, ',', 'oq'); word = substrn(string, position, length); output; end; run; proc print data=test noobs; var count position length word; run;