6.2 C
London
Thursday, December 19, 2024
HomeSASData Munging in SASHow to Use the INDEX Function in SAS (With Examples)

How to Use the INDEX Function in SAS (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the INDEX function in SAS to return the position of the first occurrence of a string within another character string.

This function uses the following basic syntax:

INDEX(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 INDEX 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
Chad Simpson Arnolds
Derrick Smith Henrys
Eric Millerton Smith
Frank Giovanni Goode
;
run;

/*view dataset*/
proc print data=original_data;

We can use the INDEX function to search for the position of the first occurrence of the string “Smith” in each row:

/*find position of first occurrence of 'Smith' in name*/
data new_data;
    set original_data;
    first_smith = index(name, 'Smith');
run;

/*view results*/
proc print data=new_data;

The new column called first_smith displays the position of the first occurrence of the string ‘Smith’ in the name column.

If ‘Smith’ is not found at all, the INDEX function simply returns a value of 0.

It’s important to note that the INDEX function is case-sensitive, so if you search for ‘smith’ instead, the INDEX function will return 0 for each string:

/*find position of first occurrence of 'smith' in name*/
data new_data;
    set original_data;
    first_smith = index(name, 'smith');
run;

/*view results*/
proc print data=new_data;

To perform a case-insensitive search, you can use the lowcase() function to first convert each string to all lowercase and then search for ‘smith’ as follows:

/*find position of first occurrence of 'smith' in name*/
data new_data;
    set original_data;
    first_smith = index(lowcase(name), 'smith');
run;

/*view results*/
proc print data=new_data;

By first converting each string to all lowercase, we’re able to use the INDEX function to perform a case-insensitive search.

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories