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 !
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%").
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.
@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+)? *%/'
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.
Ready to level-up your skills? Choose your own adventure.