• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / SAS / SAS prxmatch – Find Pattern in String Using Regex (Regular Expression)

SAS prxmatch – Find Pattern in String Using Regex (Regular Expression)

January 25, 2022 Leave a Comment

You can use the SAS prxmatch function to perform a regular expression (regex) search on a character variable in a SAS data step, and return the position where the pattern first was found.

data data_new;
   string = "This is a string with some text that we will search and replace with a regex expression.";
   pat = '/(?<!\S)\S{4}(?!\S)/'; /* pattern for word with 4 letters */
   match_location = prxmatch(pat,string);
   put match_location=;
run;

/* Output */
match_location = 4;

When working with strings in our datasets, it can be useful to find if the variables contain certain patterns and substrings.

In SAS, we can use the prxmatch() function to perform regular expression (regex) searches on character variables. The SAS prxmatch() function returns the position of where the pattern first matches in a string. The position is 1 based, and if prxmatch() doesn’t find a match, it returns 0.

data data_new;
   string = "This is a string with some text that we will search with a regex expression.";
   pat = '/(?<!\S)\S{5}(?!\S)/'; /* pattern for word with 5 letters */
   match_location = prxmatch(pat,string);
   put match_location=;
run;

/* Output */
match_location = 60;

Finding Patterns in Character Variables with prxmatch() in SAS

Regular expression (regex) searches are very powerful for finding substrings and patterns in string variables. With prxmatch() we can find both simple and complex regex patterns in our character variables.

Let’s say we have some strings and want to find all of the words that start with “S”.

We can use the following SAS code to find if the strings start with the letter “S” in the following way.

data data_new;
   string1 = "This";
   string2 = "Song";
   pat = '/^S/'; /* pattern for words that start with S */
   found1 = prxmatch(pat,string1) > 0;
   found2 = prxmatch(pat,string2) > 0;
   put found1=;
   put found2=;
run;

/* Output: */
found1 = 0;
found2 = 1;

Hopefully this article has been useful for you to learn how to use the SAS prxmatch() function to perform regex searches on character variables in SAS data steps.

Other Articles You'll Also Like:

  • 1.  SAS select when – Evaluating Character Values for Conditional Processing
  • 2.  SAS right() Function – Right Align Character Variables in Data Step
  • 3.  SAS Percent Format – Formatting Number as Percent in SAS Dataset
  • 4.  Concatenating Strings in a SAS Data Step
  • 5.  SAS substr() Function – Get Substring from Character Variable
  • 6.  SAS compress – Remove Whitespace and Characters from String
  • 7.  Get the Row Number in a SAS Data Step with _n_
  • 8.  SAS data _null_ – Create a SAS Dataset with No Records and Variables
  • 9.  SAS month function – Get Month from Date Variable
  • 10.  Remove Specific Character from String in SAS

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy