FINDC Function
Searches a string for any character in a list of characters.
| Categories: | Character |
|---|---|
| CAS | |
| Restriction: | This function is assigned an I18N Level 0 status and is designed for SBCS data. However, if the first argument, string, is specified as varchar and there are multi-byte characters, then the FINDC function processes the multi-byte data. For more information, see Internationalization Compatibility for SAS String Functions in SAS National Language Support (NLS): Reference Guide. |
| Tip: | Use the KINDEXC Function in SAS National Language Support (NLS): Reference Guide instead to write encoding independent code. |
Table of Contents
- Syntax
- Required Arguments
- Optional Argument
- Details
- Basics
- Processing SBCS and DBCS Data
- Comparisons
- Examples
- Example 1: Searching for Characters in a String
- Example 2: Searching for Characters in a String and Ignoring Case
- Example 3: Searching for Characters and Using the K Modifier
- Example 4: Searching for the Characters h, i, and Blank
- Example 5: Searching for the Characters h and i While Ignoring Case
- Example 6: Searching for the Characters h and i with Trailing Blanks Trimmed
- Example 7: Searching for All Characters, Excluding h, i, H, and I
- See Also
Syntax
Required Arguments
string
is a character constant, variable, or expression that specifies the character string to be searched.
| Tip | Enclose a literal string of characters in quotation marks. |
|---|
character-list
is a constant, variable, or character expression that initializes a list of characters. FINDC searches for the characters in this list provided that you do not specify the K modifier in the modifier argument. If you specify the K modifier, FINDC searches for all characters that are not in this list of characters. You can add more characters to the list by using other modifiers.
modifier
is a character constant, variable, or expression in which each character modifies the action of the FINDC function. The following characters, in uppercase or lowercase, can be used as modifiers:
blank
is ignored.
a or A
adds alphabetic characters to the list of characters.
b or B
searches from right to left, instead of from left to right, 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.
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 character case during the search.
k or K
searches for any character that does not appear in the list of characters. If you do not specify this modifier, then FINDC searches for any character that appears in the list of characters. This modifier has the same functionality as the v or V modifier.
l or L
adds lowercase letters to the list of characters.
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 the modifier arguments only once, rather than every time the FINDC function is called. Using the O modifier in the DATA step (excluding WHERE clauses), or in the SQL procedure can make FINDC run faster when you call it in a loop where the character-list and the modifier arguments do not change.
p or P
adds punctuation marks to the list of characters.
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 character-list arguments.
u or U
adds uppercase letters to the list of characters.
v or V
searches for any character that does not appear in the list of characters. If you do not specify this modifier, then FINDC searches for any character that appears in the list of characters. This modifier has the same functionality as the k or K modifier.
w or W
adds printable characters to the list of characters.
x or X
adds hexadecimal characters to the list of characters.
| Tip | If modifier is a constant, then enclose it in quotation marks. Specify multiple constants in a single set of quotation marks. Modifier can also be expressed as a variable or an expression. |
|---|
Optional Argument
start-position
is an optional numeric constant, variable, or expression having an integer value that specifies the position at which the search should start and the direction in which to search.
Details
Basics
The FINDC function searches string for the first occurrence of the specified characters, and returns the position of the first character found. If no characters are found in string, then FINDC returns a value of 0.
The FINDC function allows character arguments to be null. Null arguments are treated as character strings that have a length of zero. Numeric arguments cannot be null.
If start-position is not specified, FINDC begins the search at the end of the string if you use the B modifier, or at the beginning of the string if you do not use the B modifier.
If start-position is specified, the absolute value of start-position specifies the position at which to begin the search. If you use the B modifier, the search always proceeds from right to left. If you do not use the B modifier, the sign of start-position specifies the direction in which to search. The following table summarizes the search directions:
|
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, FINDC 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, the search begins at the end of the string. | |
|
equal to 0 |
returns a value of 0. | |
Processing SBCS and DBCS Data
The FINDC function is designed to process SBCS data, but it can also process DBCS data. Here are the criteria:
- If string is not declared as varchar and you are processing single-byte data, then FINDC processes SBCS.
- If string is declared as varchar and you are processing multi-byte data, then FINDC processes DBCS.
Comparisons
- The FINDC function searches for individual characters in a character string, whereas the FIND function searches for substrings of characters in a character string.
- The FINDC function and the INDEXC function both search for individual characters in a character string. However, the INDEXC function does not have the modifier nor the start-position arguments.
- The FINDC function searches for individual characters in a character string, whereas the VERIFY function searches for the first character that is unique to an expression. The VERIFY function does not have the modifier nor the start-position arguments.
Examples
Example 1: Searching for Characters in a String
This example searches a character string and returns the characters that are found.
data _null_;
string='Hi, ho!';
charlist='hi';
j=0;
do until (j=0);
j=findc(string, charlist, j+1);
if j=0 then put +3 "That's all";
else do;
c=substr(string, j, 1);
put +3 j= c=;
end;
end;
run;
SAS writes the following output to the log:
j=2 c=i j=5 c=h That's all
Example 2: Searching for Characters in a String and Ignoring Case
This example searches a character string and returns the characters that are found. The I modifier is used to ignore the case of the characters.
data _null_;
string='Hi, ho!';
charlist='ho';
j=0;
do until (j=0);
j=findc(string, charlist, j+1, "i");
if j=0 then put +3 "That's all";
else do;
c=substr(string, j, 1);
put +3 j= c=;
end;
end;
run;
SAS writes the following output to the log:
j=1 c=H j=5 c=h j=6 c=o That's all
Example 3: Searching for Characters and Using the K Modifier
This example searches a character string and returns the characters that do not appear in the character list.
data _null_;
string='Hi, ho!';
charlist='hi';
j=0;
do until (j=0);
j=findc(string, charlist, "k", j+1);
if j=0 then put +3 "That's all";
else do;
c=substr(string, j, 1);
put +3 j= c=;
end;
end;
run;
SAS writes the following output to the log:
j=1 c=H j=3 c=, j=4 c= j=6 c=o j=7 c=! That's all
Example 4: Searching for the Characters h, i, and Blank
This example searches for the three characters h, i, and blank. The characters h and i are in lowercase. The uppercase characters H and I are ignored in this search.
data _null_;
whereishi=0;
do until(whereishi=0);
whereishi=findc('Hi there, Ian!', 'hi ', whereishi+1);
if whereishi=0 then put “The End”;
else do;
whatfound=substr('Hi there, Ian!', whereishi,1);
put whereishi= whatfound=;
end;
end;
run;
SAS writes the following output to the log:
whereishi=2 whatfound=i whereishi=3 whatfound= whereishi=5 whatfound=h whereishi=10 whatfound= The End
Example 5: Searching for the Characters h and i While Ignoring Case
This example searches for the four characters h, i, H, and I. FINDC with the i modifier ignores character case during the search.
data _null_;
whereishi_i=0;
do until(whereishi_i=0);
variable1='Hi there, Ian!';
variable2='hi';
variable3='i';
whereishi_i=findc(variable1, variable2, variable3, whereishi_i+1);
if whereishi_i=0 then put "The End";
else do;
whatfound=substr(variable1, whereishi_i, 1);
put whereishi_i= whatfound=;
end;
end;
run;
SAS writes the following output to the log:
whereishi_i=1 whatfound=H whereishi_i=2 whatfound=i whereishi_i=5 whatfound=h whereishi_i=11 whatfound=I The End
Example 6: Searching for the Characters h and i with Trailing Blanks Trimmed
This example searches for the two characters h and i. FINDC with the t modifier trims trailing blanks from the string argument and the characters argument.
data _null_;
whereishi_t=0;
do until(whereishi_t=0);
expression1='Hi there, '||'Ian!';
expression2=kscan('bye or hi', 3)||' ';
expression3=trim('t ');
whereishi_t=findc(expression1, expression2, expression3, whereishi_t+1);
if whereishi_t=0 then put "The End";
else do;
whatfound=substr(expression1, whereishi_t,1);
put whereishi_t= whatfound=;
end;
end;
run;
SAS writes the following output to the log:
whereishi_t=2 whatfound=i whereishi_t=5 whatfound=h The End
Example 7: Searching for All Characters, Excluding h, i, H, and I
This example searches for all of the characters in the string, excluding the characters h, i, H, and I. FINDC with the v modifier counts only the characters that do not appear in the characters argument. This example also includes the i modifier and therefore ignores character case during the search.
data _null_;
whereishi_iv=0;
do until(whereishi_iv=0);
xyz='Hi there, Ian!';
whereishi_iv=findc(xyz, 'hi', whereishi_iv+1, 'iv');
if whereishi_iv=0 then put "The End";
else do;
whatfound=substr(xyz, whereishi_iv,1);
put whereishi_iv= whatfound=;
end;
end;
run;
SAS writes the following output to the log:
whereishi_iv=3 whatfound= whereishi_iv=4 whatfound=t whereishi_iv=6 whatfound=e whereishi_iv=7 whatfound=r whereishi_iv=8 whatfound=e whereishi_iv=9 whatfound=, whereishi_iv=10 whatfound= whereishi_iv=12 whatfound=a whereishi_iv=13 whatfound=n whereishi_iv=14 whatfound=! The End
See Also
-
Functions:
- ANYALNUM Function
- ANYALPHA Function
- ANYCNTRL Function
- ANYDIGIT Function
- ANYGRAPH Function
- ANYLOWER Function
- ANYPRINT Function
- ANYPUNCT Function
- ANYSPACE Function
- ANYUPPER Function
- ANYXDIGIT Function
- COUNTC Function
- INDEXC Function
- NOTALNUM Function
- NOTALPHA Function
- NOTCNTRL Function
- NOTDIGIT Function
- NOTGRAPH Function
- NOTLOWER Function
- NOTPRINT Function
- NOTPUNCT Function
- NOTSPACE Function
- NOTUPPER Function
- NOTXDIGIT Function
- VERIFY Function