You can use the PRXMATCH function in SAS to search for a specific pattern in a string and return the position at which the pattern is found.
This function uses the following basic syntax:
PRXMATCH(regular expression, source)
where:
- regular expression: Regular expression that specifies the pattern to search for
- source: Name of the variable to search
The following examples show three common ways to use this function in practice with the following dataset in SAS:
/*create dataset*/
data my_data;
input team $ points;
datalines;
Mavs 22
mavs 14
Warriors 23
Mavs 19
warriors 34
MAVS 40
WARRIORS 39
;
run;
/*view dataset*/
proc print data=my_data;
Example 1: Use PRXMATCH to Find Position of Pattern in String
The following code shows how to use the PRXMATCH function to create a new column called position that contains the position where the string “avs” occurs in the team column:
/*create new dataset*/
data new_data;
set my_data;
position = prxmatch("/avs/", team);
run;
/*view new dataset*/
proc print data=new_data;
From the output we can see:
- The first row contains the pattern “avs” in the team column starting in position 2 of the string.
- The second row contains the pattern “avs” in the team column starting position 2 of the string.
- The third row does not contain the pattern “avs” in the team column so a value of 0 is returned.
And so on.
Example 2: Use PRXMATCH to Check if String Contains Pattern
The following code shows how to use the PRXMATCH function to create a new column called mavs_flag that contains a value of 1 if the string in the team column contains “avs” anywhere in the string and a value of 0 if it does not:
/*create new dataset*/
data new_data;
set my_data;
if prxmatch("/Mavs/i", team) > 0 then mavs_flag = 1;
else mavs_flag = 0;
run;
/*view new dataset*/
proc print data=new_data;
Note that the i at the end of the regular expression specifies that SAS should perform a case-insensitive search.
Simply leave out the i if you’d like to perform a case-sensitive search instead.
Example 3: Use PRXMATCH to Filter Dataset for Rows that Contain Pattern
The following code shows how to use the PRXMATCH function to create a new dataset that only contains rows from my_data that have the string “Mavs” (case insensitive) in the team column:
/*create dataset*/
data original_doriginal_data;
Notice that each of the rows in the new dataset contain “Mavs” in the team column.
Additional Resources
The following tutorials explain how to perform other common tasks in SAS:
How to Extract Numbers from String in SAS
How to Remove Commas from String in SAS
How to Split Strings by Delimiter in SAS