The FIND Function in SAS (FIND v/s FINDC v/s FINDW)

The FIND function in SAS searches for a specific substring in a given string or character variable and returns the position of its first occurrence. 

The FIND function is mostly used where any specific character, keyword, or phrase you want to identify, and/or extract. It is especially helpful when you read raw, scattered, and long text data. For examples,

  • Log files,
  • Unorganised text file,
  • Web scraped data, or
  • Character variable on a SAS dataset

The above list is not limited. According to your use case and requirement you can use the Index function in multiple ways.

There are three variations of the Find function in SAS:

  • FIND
  • FINDC
  • FINDW

The syntax for all three functions are very similar but they produce somewhat different results. Here is the short description:

FIND v/s FINDC v/s FINDW in SAS

  1. INDEX – It searches a substring in a character string and returns the position of its first occurrence from the character string.
  2. INDEXC – It searches individual characters from a specified substring and returns the first occurrence of any character from a given substring.
  3. INDEXW – It searches for the exact character, word, or string mentioned in the specific string and returns the position of the first occurrence. 

Let’s understand each one of them in more detail with multiple examples.

FIND Function Syntax:

				FIND( string, substring, < modifier(s)>,  < start-position>)
			
  • string: This is your input string, or character variable that contains the substring which you want to search for. 
  • substring: It’s character substring which you want to search for in a string or character variable.
  • modifier: It can be used to ignore case or trim blank spaces from substring. You can either use “i” to ignore the case or use “t” to trim blank spaces from substring.
  • start-position: It’s a numeric parameter that gives you flexibility to mention start position from where SAS will start searching for a specified substring in a string or char variable.

Find Function Example:

Let’s start with a simple example to understand how the FIND function works in SAS. For the demonstration, let’s create a dataset named “FIND_function”.

We’ll create a new numeric variable “position_nr” which will have the position number of a specified string. We will search for a substring “sas” by ignoring the CASE of a substring in the given string “Learn SAS code for free from a SAS expert”

				data FIND_function;
	str="Learn SAS code for free from a SAS expert";
	position_nr=find(str, "sas", "i");
run;

proc print data=FIND_function;
run;
			
sas find function

Example 2:

Using the FIND function directly on the char variable is even more easy and convenient if you want to search a specific string on the entire column in sas dataset. 

We will use a dataset named “SHOES” available in the sashelp library. Observe the SHOES dataset: variable product.

We will try to find and calculate a position number where “casual” is mentioned in the product name.

				data FIND_function(keep=region product casual_position_nr);
	set sashelp.shoes;
	casual_position_nr=find(product, "casual", "i");
run;
proc print data=FIND_function;
run;
			
sas find function example

FIND v/s FINDC v/s FINDW in SAS

Along with the FIND function in SAS there are two more variations available for their own specific use cases. Here is the comparison in simple plain english:

  • The FIND function searches for substrings of characters in a character string, whereas the FINDC function searches for individual characters in a character string.
  • The FINDW function searches for the exact word in a character string where you get options to specify start position and modifier. 

It’s easy to understand this concept with examples.

To show you the exact difference between these three FIND functions in SAS, let’s go back and take the simplest example again. We will use the demo string: “Learn SAS Code for free from a SAS Expert”

With the FINDW function you need to specify its delimiter, followed by the modifier and starting position from where SAS starts searching for a given substring. Please note that a delimiter is super important here! 

				data FIND_Overview;
	demo_string="Learn SAS Code for free with a SAS Expert";
	FIND_fun=find(demo_string, "sas", "i");
	FINDC_fun=findc(demo_string, "sas", "i");
	FINDW_fun=findw(demo_string, "sas", " ", "i", 10);
run;
proc print data=FIND_Overview;
run;
			
SAS FIND FINDC and FINDW functions

FIND v/s INDEX function in SAS

The FIND function and the INDEX function both search for substrings of characters in a character string. However, the INDEX function does not have the modifier nor the start-position arguments.

The FINDC function and the INDEXC function both search for individual characters in a character string. However, the INDEXC function does not have the modifier nor the start-position arguments. Similarly you get more flexibility with FINDW compared to INDEXW.

SAS: How to find a string in a character variable?

FAQ

How to find a sub-string from a string in SAS?

The FIND function in SAS searches for a specific substring in a given string or character variable and returns the position of its first occurrence.

What are the SAS functions available to find position of a substring?

There are multiple SAS functions which you can use to find the position of a given sub string such as FIND, FINDC, FINDW, INDEX, INDEXC, or INDEXW, etc.