SASCRUNCH TRAINING
  • Home
  • SASĀ® Certification Training
    • SAS Certified Specialist Exam Training Program
    • How to Prepare For SAS Certified Specialist Base Programming Exam
  • Online Courses
    • Practical SAS Training Course for Beginners
    • Proc SQL Course
    • SAS Project Training Course
    • Logistic Regression (Credit Scoring) Modeling using SAS
  • Articles
    • Get Started >
      • 18 Free Resources to Help You Learn SAS
      • SAS Tutorial
      • How to Install SAS Studio
      • How to Learn SAS Fast
    • Data Import >
      • Importing Excel Spreadsheet
      • Importing CSV Files
      • Importing Text Files
    • SAS Functions >
      • CAT, CATT, CATS, CATX Functions
      • If-Then-Else statement
      • TRIM Function
      • STRIP Function
      • YEAR, MONTH, DAY Functions
      • Compress Function
      • Do-Loop
      • SCAN Function
      • LIKE Operator
      • INDEX Function
    • Data Manipulations >
      • The Ultimate Guide to Proc SQL
      • Proc Datasets
      • Dictionary Tables
      • Dealing with Missing Values
      • Proc Compare
      • Proc Transpose
      • RETAIN Statement
      • SAS Formats
      • SAS Arrays
    • Statistical Analysis >
      • Proc Means
      • Proc Freq
      • Proc Tabulate
    • Machine Learning >
      • Predicting Fish Species Using K-nearest Neighbor in SAS
      • Classify Product Reviews on Amazon Using Naive Bayes Model in SAS
    • Informational Interviews >
      • How to get a Clinical Trial/Research job without experience
      • Senior Recruiter at a Fortune 500 Retail Company
      • Manager, Non-profit Health Services Research
      • HR Manager
      • Quantitative Analyst
  • Services
    • The Ultimate Job Search Automation Services
    • Statistical Consulting
    • SAS Project or Assignment Help
    • Data Import Services
    • Data Manipulation and Reporting Services
  • In-class Training
    • SAS Training for Job Seekers
  • Guest Lecture
  • Sample Resume
  • About us
  • Contact Us
Master SAS
​in 30 Days
Start your Free training now!
x
Picture
Need help studying for the new
SAS Certified Specialist Exam?
Get access to:
  • Two Full Certificate Prep Courses
  • ​300+ Practice Exercises
Start your free training now
How to Prepare for the SAS Certified Specialist Base Programming Exam
Picture
 
Lesson 6.4: Index Function
IMPORTANT! We have recently updated our training materials. Check out the latest free training here. 


INDEX Function returns the starting position of the text searched.

Example
Picture
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.

DATA Student2;

Set Student;

Sam = INDEX(List, ‘Sam’);
John = INDEX(List, ‘John’);

RUN;


Picture
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:

Example

Picture
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:

DATA Student2;

Set Student;

d = index(STU_INFO, ",");

Name = Substr( STU_INFO, 1, d-1);
Phone = Substr( STU_INFO, d+1);

RUN; 

Picture
Picture
(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! 

Previous
Next

    Have a Question?

Submit
 

Master SAS in 30 Days

Start your Free training now!
Copyright © 2012-2019 SASCrunch.com All rights reserved.
  • Home
  • SASĀ® Certification Training
    • SAS Certified Specialist Exam Training Program
    • How to Prepare For SAS Certified Specialist Base Programming Exam
  • Online Courses
    • Practical SAS Training Course for Beginners
    • Proc SQL Course
    • SAS Project Training Course
    • Logistic Regression (Credit Scoring) Modeling using SAS
  • Articles
    • Get Started >
      • 18 Free Resources to Help You Learn SAS
      • SAS Tutorial
      • How to Install SAS Studio
      • How to Learn SAS Fast
    • Data Import >
      • Importing Excel Spreadsheet
      • Importing CSV Files
      • Importing Text Files
    • SAS Functions >
      • CAT, CATT, CATS, CATX Functions
      • If-Then-Else statement
      • TRIM Function
      • STRIP Function
      • YEAR, MONTH, DAY Functions
      • Compress Function
      • Do-Loop
      • SCAN Function
      • LIKE Operator
      • INDEX Function
    • Data Manipulations >
      • The Ultimate Guide to Proc SQL
      • Proc Datasets
      • Dictionary Tables
      • Dealing with Missing Values
      • Proc Compare
      • Proc Transpose
      • RETAIN Statement
      • SAS Formats
      • SAS Arrays
    • Statistical Analysis >
      • Proc Means
      • Proc Freq
      • Proc Tabulate
    • Machine Learning >
      • Predicting Fish Species Using K-nearest Neighbor in SAS
      • Classify Product Reviews on Amazon Using Naive Bayes Model in SAS
    • Informational Interviews >
      • How to get a Clinical Trial/Research job without experience
      • Senior Recruiter at a Fortune 500 Retail Company
      • Manager, Non-profit Health Services Research
      • HR Manager
      • Quantitative Analyst
  • Services
    • The Ultimate Job Search Automation Services
    • Statistical Consulting
    • SAS Project or Assignment Help
    • Data Import Services
    • Data Manipulation and Reporting Services
  • In-class Training
    • SAS Training for Job Seekers
  • Guest Lecture
  • Sample Resume
  • About us
  • Contact Us