Table of Contents
FIND and INDEX are two commonly used functions in SAS that serve similar purposes but have distinct differences. FIND is used to locate the position of a specific character or set of characters within a string, while INDEX is used to search for a specific substring and return the position of its first occurrence. FIND is more limited in its search capabilities as it can only locate exact matches, while INDEX allows for partial matches. Additionally, FIND is case-sensitive whereas INDEX is not. In summary, FIND is used for precise character matching while INDEX is used for more flexible substring matching.
SAS: The Difference Between FIND vs. INDEX
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 printdata=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 printdata=new_data;

Notice that the FIND and INDEX functions return the exact same results.
Both the find_pig and index_pig columnsdisplay 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 printdata=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.
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 printdata=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.
The following tutorials explain how to use other common functions in SAS:
Cite this article
stats writer (2024). What is the difference between FIND and INDEX in SAS?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/what-is-the-difference-between-find-and-index-in-sas/
stats writer. "What is the difference between FIND and INDEX in SAS?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/what-is-the-difference-between-find-and-index-in-sas/.
stats writer. "What is the difference between FIND and INDEX in SAS?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/what-is-the-difference-between-find-and-index-in-sas/.
stats writer (2024) 'What is the difference between FIND and INDEX in SAS?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/what-is-the-difference-between-find-and-index-in-sas/.
[1] stats writer, "What is the difference between FIND and INDEX in SAS?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. What is the difference between FIND and INDEX in SAS?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
