• 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
  • Ruby
  • 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 data _null_ – Create a SAS Dataset with No Records and Variables
  • 2.  SAS compress – Remove Whitespace and Characters from String
  • 3.  SAS Dollar Format – Formatting Numbers as Dollars in SAS Dataset
  • 4.  Remove Specific Character from String in SAS
  • 5.  Using SAS to Sum by Group with PROC MEANS
  • 6.  SAS Not In – How to Check if Variable is Not in List of Values
  • 7.  mod Function in SAS – Find Remainder of 2 Numbers After Division
  • 8.  Multiple Condition If Statements in SAS Macro Language
  • 9.  SAS if then else – Write Conditional Expressions for Multiple Conditions
  • 10.  SAS tranwrd() Function – Replace Characters in Strings in SAS Data Step

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy