Table of Contents
The INDEXC function is a useful tool in SAS that allows users to search for a specific character or set of characters within a given string of text. This function can be used to extract certain characters from a larger string, determine the position of those characters, and perform various manipulations on the text. To use the INDEXC function, one must provide the string to be searched and the character or characters to be located. This function can be particularly valuable in data analysis and text processing tasks, allowing for efficient and precise identification and manipulation of relevant information.
Use the INDEXC Function in SAS
You can use the INDEXC function in SAS to return the position of the first occurrence of any individual character within a string.
This function uses the following basic syntax:
INDEXC(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 INDEXC 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 INDEXC function to search for the position of the first occurrence of the characters x, yorz:
/*find position of first occurrence of either x, y or z in name*/
data new_data;
set original_data;
first_xyz = indexc(name, 'xyz');
run;
/*view results*/
proc printdata=new_data;

The new column called first_xyz displays the position of the first occurrence of the characters x, yorz in the name column.
If none of these three characters are present in the name column, then the INDEXC function simply returns a value of 0.
For example, from the output we can see:
The position of the first occurrence of x, y or z in the first row is in position 4. We can see that the character in position 4 in the first row is a y.
And so on.
The Difference BETWEEN INDEX and INDEXC Functions
The INDEX function in SAS returns the position of the first occurrence of a particular substring in another string.
The following example illustrates the difference between the INDEX and INDEXC functions:
/*create new dataset*/
data new_data;
set original_data;
index_smith = index(name, 'Smith');
indexc_smith = indexc(name, 'Smith');
run;
/*view new dataset*/
proc printdata=new_data;
The index_smith column displays the position of the first occurrence of the substring ‘Smith’ in the name column.
The indexc_smith column displays the position of the first occurrence of the letters s, m, i, torh in the name column.
For example, from the output we can see:
The substring ‘Smith’ never occurs in the first name so index_smith returns a value of 0.
The letter i occurs in the 7th position of the first name so indexc_smith returns a value of 7.
And so on.
The following tutorials explain how to use other common functions in SAS:
Cite this article
stats writer (2024). How can I use the INDEXC function in SAS?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-the-indexc-function-in-sas/
stats writer. "How can I use the INDEXC function in SAS?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-the-indexc-function-in-sas/.
stats writer. "How can I use the INDEXC function in SAS?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-the-indexc-function-in-sas/.
stats writer (2024) 'How can I use the INDEXC function in SAS?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-the-indexc-function-in-sas/.
[1] stats writer, "How can I use the INDEXC function in SAS?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I use the INDEXC function in SAS?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
