The SAS find() function allows us to check if a given substring of characters is in a character variable in a SAS data step.
data k;
a = 'this is a string with some characters';
b = find(a,"str");
put b=;
run;
/* Output: */
b=11
When working with datasets which contain string and character variables, being able to check if a string contains another string can be very useful.
The SAS find() function allows us to check and see if a character variable contains a given substring of characters.
find() takes in two required arguments, the character variable you want to check and a given substring of characters.
The SAS find() function returns the position of where the substring occurs in the character variables, where 1 is the first position.
Below is a simple example showing how you could use find() to find characters in a string and get the position of the characters.
data k;
a = 'this is a string with some characters';
b = find(a,"str");
put b=;
run;
/* Output: */
b=11
If the given substring is not in the string, then find() returns 0.
data k;
a = 'this is a string with some characters';
b = find(a,"other");
put b=;
run;
/* Output: */
b=0
Checking if Substring is in String with SAS find() Function
The SAS find() function has a few optional arguments which allow us to modify the behavior of the returned value.
First, we can pass ‘i’ to find() to ignore the case of the string.
For example, if our string has a mix of uppercase and lowercase characters, then passing ‘i’ could be beneficial.
Below is an example of how to use find() ignoring the case of the characters.
data k;
a = 'this is a string with some characters';
b = find(a,"IS",'i');
put b=;
run;
/* Output: */
b=6
Another modification you can make is if you want to remove the trailing and leading blanks before determining the position of a substring.
You can pass ‘t’ to remove trailing and leading blanks before finding the position of the substring.
Below is an example of using the ‘t’ modifier with the SAS find() function.
data k;
a = ' this is a string with some leading and trailing blanks ';
b = find(a,"is",'t');
put b=;
run;
/* Output: */
b=6
Finally, we can adjust the starting position of where we start the search of the string. By default, find() starts from the beginning of the string and works right.
But you can start from the end of the string and work left, or start from some other position and work right.
If you pass a number greater than 0, find() will start at that position and work right. If you pass a negative number, find() will start at that position and work left.
By using the ‘startpos’ argument, you can skip over potentially unwanted matches for find().
Below is an example of using the ‘startpos’ argument with find().
data k;
a = 'this is a string with some characters';
b = find(a,"th",10);
put b=;
run;
/* Output: */
b=20
Hopefully this article has been useful for you to learn how to use the SAS find() function in a SAS data step.
Leave a Reply