You can use both the FIND and INDEX functions in SAS to return the position of the first character of a substring that occurs within a string.
The difference between these functions is that the FIND function allows you to do two things that the INDEX function cannot do:
- FIND allows you to perform a case-insensitive search.
- FIND allows you to specify a starting position for searching.
The following examples illustrate the difference between the FIND and INDEX functions in practice with the following dataset in SAS hat contains a column of phrases:
/*create dataset*/
data original_data;
input phrase $40.;
datalines;
A pig is my favorite animal
My name is piglet
Pigs are so cute
Here is a baby pig
His name is piggie
;
run;
/*view dataset*/
proc print data=original_data;
Example 1: Using FIND and INDEX with No Differences
The following code shows how to use both the FIND and INDEX functions to search for the position of the first occurrence of the substring ‘pig’ in the phrase column:
/*find position of first occurrence of 'pig' in phrase column*/
data new_data;
set original_data;
find_pig = find(phrase, 'pig');
index_pig = index(phrase, 'pig');
run;
/*view results*/
proc print data=new_data;
Notice that the FIND and INDEX functions return the exact same results.
Both the find_pig and index_pig columns display the position of the first occurrence of the substring ‘pig’ in the phrase column.
Example 2: Using FIND and INDEX with Case-Insensitive Search
The following code shows how to use both the FIND and INDEX functions to search for the position of the first occurrence of the word ‘PIG’ in the phrase column:
/*find position of first occurrence of 'PIG' in phrase column*/
data new_data;
set original_data;
find_pig = find(phrase, 'PIG', 'i');
index_pig = index(phrase, 'PIG');
run;
/*view results*/
proc print data=new_data;
By using the ‘i’ modifier in the FIND function, we were able to perform a case-insensitive search of the substring ‘PIG’ in the phrase column.
However, the INDEX function is not able to perform a case-insensitive search so it simply returned 0 for each row since the uppercase substring ‘PIG’ did not exist in any phrase.
Example 3: Using FIND and INDEX with Specific Starting Position
The following code shows how to use the FIND function to search for the substring ‘pig’ in the phrase column starting at position 5 while the INDEX function is not capable of using a specific starting position at all:
/*find position of first occurrence of 'pig' in phrase column starting at position 5*/
data new_data;
set original_data;
find_pig = find(phrase, 'pig', 5);
index_pig = index(phrase, 'pig');
run;
/*view results*/
proc print data=new_data;
The find_pig function searches for the substring ‘pig’ starting from position 5 of the phrase column.
The index_pig function simply searches for the substring ‘pig’ anywhere in the phrase column since it is not capable of specifying a starting position for searching.
Additional Resources
The following tutorials explain how to use other common functions in SAS:
How to Use the SUBSTR Function in SAS
How to Use the COMPRESS Function in SAS
How to Use the COALESCE Function in SAS