BookmarkSubscribeRSS Feed
SASdevAnneMarie
Barite | Level 11

Hello Experts,

 

I have a data like X="BAB 1,50%", I apply the prxmatch function 

if prxmatch("/\b1(\.50\%|\%|\s)/",X)>0

but I don't have the right result because I have in the output also the data with X="BAB 1".  How to improve the code to get the values with "%" ?

 

Thank you !

4 REPLIES 4
FreelanceReinh
Jade | Level 19

Hello @SASdevAnneMarie,




@SASdevAnneMarie wrote:
if prxmatch("/\b1(\.50\%|\%|\s)/",X)>0

... I have in the output also the data with X="BAB 1".


This is because you didn't require a percent sign, but allowed a space character (\s) instead. You may want to allow one or more space characters between the number and the percent sign:

/\b1(\.50)?\s*%/

This matches a word boundary (\b) followed by the number "1" or "1.50", followed by a percent sign either directly or after one or more space characters (e.g. "1  %").

 

Note that the percent sign does not need to be escaped with a backslash ("\%") and that the expression would not match a number using a comma instead of the decimal point ("1,50%").

SASdevAnneMarie
Barite | Level 11
Thank you, FreelanceReinh, the right function is "/\b1(\.50)?\s*%/" ?
FreelanceReinh
Jade | Level 19

Whether my suggested regular expression is "right" depends on your requirements, which are not exhaustively defined by the two example strings "BAB 1,50%" (to be matched; or rather with a decimal point?) and "BAB 1" (not to be matched). If my description

 

This matches a word boundary (\b) followed by the number "1" or "1.50", followed by a percent sign either directly or after one or more space characters (e.g. "1  %").

is what you want, then, yes, the regex is right.

Patrick
Opal | Level 21

@SASdevAnneMarie wrote:
Thank you, FreelanceReinh, the right function is "/\b1(\.50)?\s*%/" ?

I would be using single quotes for SAS to not interpret the percent sign as macro token. 

 

In case you also want to cover values like 1.40, 1.3, 1.233333 etc. you could change the RegEx to

'/\b1(\.\d+)? *%/'

or even a bit broader to cover any percent values like 2 %, 12.5 $, 3.765333 %

'/\b\d+(\.\d+)? *%/'

I've replaced \s with a blank. \s stands for any whitespace character like tab etc. which I felt should not be included.

 

And of course if the string must start with BAB then the RegEx needs to include it.

'/^BAB +\d+(\.\d+)? *%/'

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 652 views
  • 2 likes
  • 3 in conversation