Sentry Page Protection
SAS Functions [11-14]
INDEXC Function
The INDEXC function is similar to the INDEX function. However, it searches for the individual character instead of the whole text.
Let's take a look at the example below.
The NAME data set above contains a list of first names.
[Reminder: To see the NAME data set on SAS Studio, run the code in the yellow box above]
The first name should contain only letters. It should not contain any other symbols.
The INDEXC function can be used to find out whether any unwanted characters are found from the text.
Example
Data Name2;
Set Name;
Indc = Indexc(FirstName, "!@#$%^&*()<>?");
Run;
The INDEXC function will scan the variable for each of the characters listed in the second parameter (i.e. !@#$%^&*()<>?).
In this example, one of the name is captured as "Terry###".
This is obviously an error, as the name should not contain any "#" sign.
The INDEXC function scans the FirstName variable and find the "#" sign at the 6th position.
As a result, it returns the value of 6.
The INDEXC function is commonly used in the data cleansing process.
It allows you to check whether any incorrect characters are captured in any character variables.
Exercise
Copy and run the MATHEXAM data set from the yellow box below.
Write a SAS program to extract the numeric exam result from the COMMENT variable.
Use the INDEXC function instead of the INDEX function when performing the extraction.
Create any data set or variables if necessary.
Need some help?
HINT:
Use the INDEXC function to search for numbers. Use the result in a SUBSTR function for proper extraction.
SOLUTION:
Data MathExam2;
Set MathExam;
Ind = Indexc(Comment, "1234567890");
Result = substr(Comment, ind);
Run;
Fill out my online form.