Table of Contents
The INDEX function in SAS is a powerful tool that allows users to search for a specific character or substring within a given string. This function returns the position of the first occurrence of the specified character or substring, making it useful for data manipulation and analysis.
The INDEX function can be used in SAS by providing the string to be searched and the character or substring to be found as parameters. It then returns the position of the first occurrence of the specified character or substring within the given string. This function can be used in various SAS procedures and functions, such as the DATA step, SQL procedure, and the WHERE statement.
For example, if we have a dataset containing customer names and we want to extract only those names that contain the letter “a”, we can use the INDEX function in the WHERE statement as follows:
WHERE INDEX(name, ‘a’) > 0;
This will return all the rows where the name column contains the letter “a”. We can also use the INDEX function in the DATA step to create a new variable that indicates whether a specific character or substring is present in a given string.
Overall, the INDEX function in SAS is a versatile and useful tool for data manipulation and analysis, and its applications are not limited to the above examples. It can be used in various scenarios to efficiently extract and manipulate data based on specific character or substring patterns.
Use the INDEX Function in SAS (With Examples)
You can use the INDEX function in SAS to return the position of the first occurrence of a string within another character string.
This function uses the following basic syntax:
INDEX(source, excerpt)
where:
- source: The string to analyze
- excerpt: The string of characters to search for within source
The following example shows how to use this function in practice.
Example: Using the INDEX Function in SAS
Suppose we have the following dataset in SAS that contains a column of names:
/*create dataset*/
data original_data;
input name $25.;
datalines;
Andy Lincoln Bernard
Barren Michael Smith
Chad Simpson Arnolds
Derrick Smith Henrys
Eric Millerton Smith
Frank Giovanni Goode
;
run;
/*view dataset*/
proc printdata=original_data;
We can use the INDEX function to search for the position of the first occurrence of the string “Smith” in each row:
/*find position of first occurrence of 'Smith' in name*/
data new_data;
set original_data;
first_smith = index(name, 'Smith');
run;
/*view results*/
proc printdata=new_data;

The new column called first_smith displays the position of the first occurrence of the string ‘Smith’ in the name column.
If ‘Smith’ is not found at all, the INDEX function simply returns a value of 0.
It’s important to note that the INDEX function is case-sensitive, so if you search for ‘smith’ instead, the INDEX function will return 0 for each string:
/*find position of first occurrence of 'smith' in name*/
data new_data;
set original_data;
first_smith = index(name, 'smith');
run;
/*view results*/
proc printdata=new_data;
To perform a case-insensitive search, you can use the function to first convert each string to all lowercase and then search for ‘smith’ as follows:
/*find position of first occurrence of 'smith' in name*/
data new_data;
set original_data;
first_smith = index(lowcase(name), 'smith');
run;
/*view results*/proc printdata=new_data;
By first converting each string to all lowercase, we’re able to use the INDEX function to perform a case-insensitive search.
The following tutorials explain how to use other common functions in SAS:
Cite this article
stats writer (2024). How can the INDEX function be used in SAS, and can you provide some examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-index-function-be-used-in-sas-and-can-you-provide-some-examples/
stats writer. "How can the INDEX function be used in SAS, and can you provide some examples?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-the-index-function-be-used-in-sas-and-can-you-provide-some-examples/.
stats writer. "How can the INDEX function be used in SAS, and can you provide some examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-index-function-be-used-in-sas-and-can-you-provide-some-examples/.
stats writer (2024) 'How can the INDEX function be used in SAS, and can you provide some examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-index-function-be-used-in-sas-and-can-you-provide-some-examples/.
[1] stats writer, "How can the INDEX function be used in SAS, and can you provide some examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can the INDEX function be used in SAS, and can you provide some examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
