FIND Function

Searches for a specific substring of characters within a character string.

Category: Character
Restriction: I18N Level 1 functions should be avoided, if possible, if you are using a non-English language. The I18N Level 1 functions might not work correctly with Double Byte Character Set (DBCS) or Multi-Byte Character Set (MBCS) encodings under certain circumstances.
Tip: Use the KINDEX Function in SAS National Language Support (NLS): Reference Guide instead to write encoding independent code.

Syntax

Required Arguments

string

specifies a character constant, variable, or expression that will be searched for substrings.

Tip Enclose a literal string of characters in quotation marks.

substring

is a character constant, variable, or expression that specifies the substring of characters to search for in string.

Tip Enclose a literal string of characters in quotation marks.

Optional Arguments

modifiers

is a character constant, variable, or expression that specifies one or more modifiers. The following modifiers can be in uppercase or lowercase:

i

ignores character case during the search. If this modifier is not specified, FIND only searches for character substrings with the same case as the characters in substring.

t

trims trailing blanks from string and substring.

Note: If you want to remove trailing blanks from only one character argument instead of both (or all) character arguments, use the TRIM function instead of the FIND function with the T modifier.
Tip
If modifier is a constant, 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.

startpos

is a numeric constant, variable, or expression with an integer value that specifies the position at which the search should start and the direction of the search.

Details

The FIND function searches string for the first occurrence of the specified substring, and returns the position of that substring. If the substring is not found in string, FIND returns a value of 0.
If startpos is not specified, FIND starts the search at the beginning of the string and searches the string from left to right. If startpos is specified, the absolute value of startpos determines the position at which to start the search. The sign of startpos determines the direction of the search.
Value of startpos
Action
greater than 0
starts the search at position startpos and the direction of the search is to the right. If startpos is greater than the length of string, FIND returns a value of 0.
less than 0
starts the search at position –startpos and the direction of the search is to the left. If –startpos is greater than the length of string, the search starts at the end of string.
equal to 0
returns a value of 0.

Comparisons

  • The FIND function searches for substrings of characters in a character string, whereas the FINDC function searches for individual characters in a character string.
  • The FIND function and the INDEX function both search for substrings of characters in a character string. However, the INDEX function does not have the modifiers nor the startpos arguments.

Example

The following SAS statements produce these results.
SAS Statement
Result
whereisshe=find('She sells seashells? Yes, she does.','she ');
put whereisshe;
27
variable1='She sells seashells? Yes, she does.';
variable2='she ';
variable3='i';
whereisshe_i=find(variable1,variable2,variable3);
put whereisshe_i;
1
expression1='She sells seashells? '||'Yes, she does.';
expression2=kscan('he or she',3)||'  ';
expression3=trim('t   ');
whereisshe_t=find(expression1,expression2,expression3);
put whereisshe_t;
14
xyz='She sells seashells? Yes, she does.';
startposvar=22;
whereisshe_22=find(xyz,'she',startposvar);
put whereisshe_22;
27
xyz='She sells seashells? Yes, she does.';
startposexp=1-23;
whereisShe_ineg22=find(xyz,'She','i',startposexp);
put whereisShe_ineg22;
14