SAS FIND Function: Learn with Examples

Deepanshu Bhalla Add Comment

In this tutorial, we will show how to use the FIND Function in SAS, along with examples.

What does FIND Function do in SAS?

The FIND function is used in SAS to search for a specific substring within a string. It returns the position of the first occurrence of the substring within the string.

Syntax of FIND Function

Below is the syntax of FIND Function in SAS.

FIND( string, substring , [modifier], [start-position])
  • string: The character variable or string that you want to search.
  • substring: The character variable or string that you want to find within the string.
  • modifier (optional): Specifies the type of search to be performed. It can be any of the following:
    • i: Performs a case-insensitive search.
    • t: Trims trailing blanks from string and substring.
  • start-position (optional): Specifies the position in the string where the search should start. It can be a positive or negative integer. A positive value starts the search from the beginning of the string, while a negative value starts the search from the end of the string.
Sample Dataset

Let's create a sample dataset for demonstration purpose.

data mydata;
input text $22.;
datalines;
What is your name?
His name sounds cool
He looks sharper
His name is David
Name:What's in a name
;
run;
SAS FIND Function

Examples: FIND Function

In this section, we will cover various examples of the FIND function to gain a better understanding of how it works.

Find Position of First Occurrence of String

Here we are using the FIND function to search for the occurrence of "name" within the variable "text".

data newdata;
set mydata;
first_name = find(text, "name");
run;
SAS FIND Function Examples
  • "What is your name?": The first occurrence of "name" is at position 14 in the text.
  • "His name sounds cool": The first occurrence of "name" is at position 5 in the text.
  • "He looks sharper": The word "name" is not present in this text, so the result is 0, indicating no occurrence.
  • "His name is David": The first occurrence of "name" is at position 5 in the text.
  • "Name: What's in a name": The first occurrence of "name" is at position 18 in the text. Please note it is case-sensitive.

The position of the first occurrence is stored in the variable "first_name" in the new dataset "newdata.

Find Case-Insensitive First Occurrence Position

To make the search case-insensitive i.e. treating uppercase and lowercase letters as equal, we can use the modifier i in the FIND function.

data newdata;
set mydata;
first_name = find(text, "name", "i");
run;
SAS FIND: Case-Insensitive

Look at the last observation - "Name: What's in a name". It has two occurrences of "name". After enabling case-insensitive search, it returns 1 instead of 18.

Customize Search with Custom Start Position

In the code below, the number 6 represents the position in the text string from where the search should start.

data newdata;
set mydata;
first_name = find(text, "name", "i", 6);
run;
SAS Find: Custom Start Position

It returns 0 for second and fourth row as "name" in these two rows are at position 5.

Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 0 Response to "SAS FIND Function: Learn with Examples"
Next → ← Prev