Lesson 6.4: Index Function
INDEX Function returns the starting position of the text searched.
In this dataset, we have a text variable called 'List' containing the list of students who achieved excellent in the SAS course. I want to see if my name, Sam, is on the list! I also want to see my friend, John, is on the list as well. I am going to use the index function to check it out.
Sam = INDEX(List, ‘Sam’);
John = INDEX(List, ‘John’);
As you can see, the variable Sam contains a result of 6, which is the starting position of the text ‘Sam’ in the variable List. The variable John returns the result of 0, which means the text ‘John’ is not found in the variable List.
Use of Index function in combination of SUBSTR
The combination of INDEX and SUBSTR are very useful in separating two different text within a variable when the positions of the 'separator' are not the same across all the fields.
Let’s take a look at the following example:
The STU_INFO variable contains the Name and Phone Number of each students separated by a comma ( , ). We cannot use the SUBSTR function directly to separate the two pieces of information because the position of the comma ( , ) is not the same across the field (i.e. the comma is at the 4th position in the first row and 5th position in the second row).
We can do the following instead:
d = index(STU_INFO, ",");
Name = Substr( STU_INFO, 1, d-1);
Phone = Substr( STU_INFO, d+1);
(1) The arbitrary variable 'd' returns the position of the comma ( , ) for each row. In our example, it contains a value of '4' in the first row. It means that the comma ( , ) is found at the 4th position in the text STU_INFO. Similarly, 'd' contains a result of 5 in the second row. It means the comma is found at the 5th position in the STU_INFO variable.
(2) Combining the use of the SUBSTR function and the comma position we got from the INDEX function, we were able to separate the Name and Phone Number from the STU_INFO.
DONE! You have learned the INDEX function in SAS!