# How to 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
Derrick Smith Henrys
Eric Millerton Smith
Frank Giovanni Goode
;
run;

/*view dataset*/
proc print data=original_data;``` We can use the INDEXC function to search for the position of the first occurrence of the characters x, y or z:

```/*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 print data=new_data;
``` The new column called first_xyz displays the position of the first occurrence of the characters x, y or z 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.

The position of the first occurrence of x, y or z in the second row is 0 because none of these three letters exist in the name in the second row.

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 print data=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, t or h 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.