FINDW Function
Returns the character position of a word in a string, or returns the number of the word in a string.
| Categories: | Character |
| CAS | |
| Restriction: | This function is assigned an I18N Level 1 status and is designed for SBCS data. However, if the first argument, string, has multi-byte characters, then the FINDW function processes the DBCS data. For more information, see Internationalization Compatibility. |
Syntax
Required Arguments
string
is a character constant, variable, or expression that specifies the character string to be searched.
word
is a character constant, variable, or expression that specifies the word to search for in string.
character
is an optional character constant, variable, or expression that initializes a list of characters.
start-position
is an optional numeric constant, variable, or expression with an integer value that specifies the position at which the search should begin and the direction in which to search.
modifier
specifies a character constant, variable, or expression in which each non-blank character modifies the action of the FINDW function.
blank
is ignored.
a or A
adds alphabetic characters to the list of characters.
b or B
scans from right to left instead of from left to right, regardless of the sign of the start-position argument.
c or C
adds control characters to the list of characters.
d or D
adds digits to the list of characters.
e or E
counts the words that are scanned until the specified word is found, instead of determining the character position of the specified word in the string. Fragments of a word are not counted.
f or F
adds an underscore and English letters (that is, the characters that can begin a SAS variable name using VALIDVARNAME=V7) to the list of characters.
g or G
adds graphic characters to the list of characters.
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. If K is not specified, then all characters that are in the list of characters are treated as delimiters.
l or L
adds lowercase 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.
n or N
adds digits, an underscore, and English letters (that is, the characters that can appear after the first character in a SAS variable name using VALIDVARNAME=V7) to the list of characters.
o or O
processes the character and modifier arguments only once, rather than every time the FINDW function is called. Using the O modifier in the DATA step (excluding WHERE clauses), or in the SQL procedure, can make FINDW run faster when you call it in a loop where the character and modifier arguments do not change.
p or P
adds punctuation marks to the list of characters.
q or Q
ignores delimiters that are inside substrings that are enclosed in quotation marks. If the value of the string argument contains unmatched quotation marks, then scanning from left to right produces different words than scanning from right to left.
r or R
removes leading and trailing delimiters from the word argument.
s or S
adds space characters (blank, horizontal tab, vertical tab, carriage return, line feed, and form feed) to the list of characters.
t or T
trims trailing blanks from the string, word, and character arguments.
u or U
adds uppercase letters to the list of characters.
w or W
adds printable characters to the list of characters.
x or X
adds hexadecimal characters to the list of characters.
Details
Definition of “Delimiter”
Definition of “Word”
-
bounded on the left by a delimiter or the beginning of the string
-
bounded on the right by a delimiter or the end of the string
Searching for a String
-
If you specify the E modifier, then FINDW returns the number of complete words that were scanned while searching for the specified word. If start-position specifies a position in the middle of a word, then that word is not counted.
-
If you do not specify the E modifier, then FINDW returns the character position of the substring that is found.
|
Value of startpos
|
Action
| |
|---|---|---|
|
greater than 0
|
search begins at position start-position and
proceeds to the right. If start-position is
greater than the length of the string, then FINDW returns a value
of 0.
| |
|
less than 0
|
search begins at position
–start-position and
proceeds to the left. If start-position is
less than the negative of the length of the string, then the search
begins at the end of the string.
| |
|
equal to 0
|
FINDW returns a value
of 0.
| |
Using the FINDW Function in ASCII and EBCDIC Environments
-
If your computer uses ASCII characters, then the default delimiters are as follows:blank ! $ % & ( ) * + , - . / ; < ^ |In ASCII environments that do not contain the ^ character, the FINDW function uses the ~ character instead.
-
If your computer uses EBCDIC characters, then the default delimiters are as follows:blank ! $ % & ( ) * + , - . / ; < ¬ | ¢
Using Null Arguments
Processing SBCS and DBCS Data
-
If string is not declared as varchar or you are processing single-byte data, then FINDW processes SBCS.
-
If string is declared as varchar and you are processing multi-byte data, then FINDW processes DBCS.
Examples
Example 1: Searching a Character String for a Word
data _null_;
whereisshe=findw('She sells sea shells? Yes, she does.', 'she');
put whereisshe=;
run;whereisshe=28
Example 2: Searching a Character String and Using the Character and Start-position Arguments
data _null_;
result=findw('At least 2.5 meters of rain falls in a rain forest.',
'rain', ' ', 25);
put result=;
run;result=40
Example 3: Searching a Character String and Using the I Modifier and the Start-position Argument
data _null_;
string='Artists from around the country display their art at
an art festival.';
result=findw(string, 'Art', ' ', 'i', 10);
put result=;
run;result=47
Example 4: Searching a Character String and Using the E Modifier
data _null_;
string='Artists from around the country display their art at
an art festival.';
result=findw(string, 'art', ' ', 'E');
put result=;
run;result=8
Example 5: Searching a Character String and Using the E Modifier and the Start-position Argument
data _null_;
string='Artists from around the country display their art at
an art festival.';
result=findw(string, 'art', ' ', 'E', 50);
put result=;
run;result=3
Example 6: Searching a Character String and Using Two Modifiers
data _null_;
string='The Great Himalayan National Park was created in 1984. Because
of its terrain and altitude, the park supports a diversity
of wildlife and vegetation.';
result=findw(string, 'park', ' ', 'I E');
put result=;
run;result=5
Example 7: Searching a Character String and Using the R Modifier
data _null_;
string='Artists from around the country display their art at
an art festival.';
word=' art ';
result=findw(string, word, ' ', 'R');
put result=;
run; result=47