You can use the INDEXW function in SAS to return the position of the first character of a word that occurs within a string.
This function uses the following basic syntax:
INDEXW(source, excerpt)
where:
- source: The string to analyze
- excerpt: The word to search for within source
The following example shows how to use this function in practice.
Example: Using the INDEXW Function in SAS
Suppose we have the following dataset in SAS that 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;
We can use the INDEXW function 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;
indexw_pig = indexw(phrase, 'pig');
run;
/*view results*/
proc print data=new_data;
The new column called indexw_pig displays the position of the first occurrence of the word ‘pig’ in the phrase column.
If the word ‘pig’ never occurs in the phrase column then the INDEXW function simply returns a value of 0.
For example, from the output we can see:
The position of the first occurrence of the word ‘pig’ in the first phrase is 3.
The second row does not contain the word ‘pig’ by itself in the phrase, so a value of 0 is returned.
And so on.
The Difference BETWEEN INDEX and INDEXW Functions
The INDEX function in SAS returns the position of the first occurrence of a particular substring in another string.
By contrast, the INDEXW function returns the position of the first occurrence of a particular word in another string.
By definition, a word must have a space before and after it.
The following example illustrates the difference between the INDEX and INDEXW functions:
/*create new dataset*/
data new_data;
set original_data;
index_pig = index(phrase, 'pig');
indexw_pig = indexw(phrase, 'pig');
run;
/*view new dataset*/
proc print data=new_data;
The index_pig column displays the position of the first occurrence of the substring ‘pig’ in the phrase column.
The indexw_pig column displays the position of the first occurrence of the word ‘pig’ in the phrase column.
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 FIND Function in SAS
How to Use the COALESCE Function in SAS