SAS: How to check if a sub-string is present on a char variable in SAS?

This is a very common use case where we need to write code to check if a substring is present on a character variable in SAS.

That substring can be a character string, a character, or any special character. Depending on the outcomes you can perform your specific operations. 

You can achieve this by using different methods. 

Both the above functions search for a substring and return the position number of its first occurrence. If the substring is not found then it returns zero. 

Hence it is fairly easy to apply filters on returned position values and check if a substring is present on a character variable in SAS.

Example 1: SAS Check if a substring is present in the character string

Simple example where we will search substring “SAS” in the demo string: “Learn SASCode from a SAS expert”. On the output dataset you can easily filter data on the basis of index_position_nr column.

You can set, index_position_nr NOT EQUALs to 0 and you’ll find whether a given substring is present in the string or not.

data INDEX_Function;
	demo_str="Learn SASCode from a SAS expert";
	index_position_nr=index(demo_str, "SAS");
run;
proc print data=INDEX_Function;
run;
check if substring is present in the string - SAS Find function

Similarly you can get the same output using the FINDw function. The only thing is you get more flexibility while searching substring because it allows some additional parameters such as delimiter, modifier, and starting-position from where SAS starts searching the substring.

In the below example I have specified starting position is 10th, modifier “i” that ignores the case, and bank “ “ as a delimiter.

data INDEX_Function;
	demo_str="Learn SASCode from a SAS expert";
	index_position_nr=findw(demo_str, "SAS", "", "i", 10);
run;

proc print data=INDEX_Function;
run;
check if substring is present in SAS Findw function

Example 2: SAS Check if a substring is present on a Char Variable

Here is the by far the best use case of the INDEX or FIND function. In fact you can use any variation of the INDEX or FIND functions according to your need.

Here I’m trying to search text “Dress” on the character variable “product” using the INDEX and FIND function.

Below is a dataset created by filtering out the data which has text “Dress” in their product name by using where clause index_nr >0

data INDEX_function(where=(index_nr  >0));
	set sashelp.shoes;
	index_nr=index(product, "Dress");
run;

proc print data=index_function;
run;
check if substring is present in SAS INDEX function

Similarly, you can do the same with FINDw function to check if a substring is present on the character variable.

data FIND_function(where=(find_nr  >0));
	set sashelp.shoes;
	find_nr=findw(product, "Dress", "", "i");
run;

proc print data=find_function;
run;
check if substring is present in SAS INDEX function

Example 3: SAS Check if a special character is present in the char Variable

For this use case we will use the AFMSG dataset available in the sashelp library. AFMSG dataset data looks like this:

SAS help AFMSG dataset

Let’s try to check if the character variable TEXT has a special character “%” in it and extract only those rows.

All you need to do is use either INDEX or FIND function and apply where clause index_nr > 0

data INDEX_function (where=(index_nr >0));
	set sashelp.AFMSG;
	index_nr=index(TEXT, "%");
Run;

proc print data=INDEX_function;
run;
check if substring is present in SAS using INDEX function

FAQ

How to identify if special character appears on any char variable in SAS?

To identify special character present in the char variable you either use FIND or INDEX function. 

Here is one example:

data INDEX_function (where=(index_nr >0)); 
set sashelp.AFMSG;
index_nr=index(TEXT, "%");
run;
How to find if substring is present on a char variable in SAS?

Sub-strings can be easily identified using either FIND or INDEX functions in SAS.