In SAS you can use the function FIND to check whether a string contains a combination of characters (or a single character). If this combination of characters is found, the FIND function returns the position of the first occurrence in the string. If the substring is not found, SAS returns a 0. The syntax of the function is the following:
FIND(string, substring)
In the example below, the FIND function checks whether a string contains the substring Ms.
data work.ds;
input string $1-30;
datalines;
Mr. Jones and Ms. Smith
Ms. Smith and Mr. Johnsen
Mr. Jones and Mr. Johnsen
Ms. Smith and Ms. Taylor
;
run;
data work.ds_find;
set work.ds;
string_find = find(string, 'Ms.');
run;
By default, the FIND function is case sensitive. However, you can add a third parameter to the function to ignore the character case. So:
FIND(string, substring, ‘i’)
If you want to Find & Replace a substring, you can use the TRANWRD function.
You can find the official SAS documentation about the FIND function here.
6 thoughts on “Find a Substring in SAS with the FIND function”
Comments are closed.