SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.

Hello Experts,

 

To find 1,50% in the data string I apply the function prxmatch("/\b1,50%|\b1.750%/",line)>0.

I get a correct result for 1,50% but when I have 1.50% this function doesn't work (can't find the 1.50%). I don't know if my code is correct.

 

Thank you for your help.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

In regular expressions period has special meaning, it will match any single character.  If you want to match an actual period you need to code \. in the regular expression.

 

If you want to match any of a series of characters include them in a set of square brackets.

 

Example:

data test;
  input string $char40.;
  loc = prxmatch('/\b1[,\.]50%|\b1[,\.]75%/',string);
cards;
1.50%
1,50%
1.75%
1,75%
1.5%
;

Result

Screenshot 2025-03-08 at 2.36.26 PM.png

View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

In regular expressions period has special meaning, it will match any single character.  If you want to match an actual period you need to code \. in the regular expression.

 

If you want to match any of a series of characters include them in a set of square brackets.

 

Example:

data test;
  input string $char40.;
  loc = prxmatch('/\b1[,\.]50%|\b1[,\.]75%/',string);
cards;
1.50%
1,50%
1.75%
1,75%
1.5%
;

Result

Screenshot 2025-03-08 at 2.36.26 PM.png

SASdevAnneMarie
Barite | Level 11
Thank you, Tom!
As I understand, if I write prxmatch("/\b1.50%/",line)>0 the function is looking for 1 or 50?
Tom
Super User Tom
Super User

@SASdevAnneMarie wrote:
Thank you, Tom!
As I understand, if I write prxmatch("/\b1.50%/",line)>0 the function is looking for 1 or 50?

No.  It will be look for things like:

1.50%
1,50%
1A50%
1x50%

If you want to look for 1 or 50 you would use | just like your original post used | so it could search for 1,50% or 1.750% 

 

Here is code that would make words that consist of only 1 or 50.

 prxmatch('/\b(1|50)\b/',line)
SASdevAnneMarie
Barite | Level 11
Thank you, Tom.
I not understand the first explication "In regular expressions period has special meaning, it will match any single character. "
SASdevAnneMarie
Barite | Level 11
Thank you ! If I need to get 1.50 or 1.50 % or 1.50% can I write prxmatch("/\b1[,\.]50\s*%/",line)>0 ?
s*% is right ?
Tom
Super User Tom
Super User

@SASdevAnneMarie wrote:
Thank you ! If I need to get 1.50 or 1.50 % or 1.50% can I write prxmatch("/\b1[,\.]50\s*%/",line)>0 ?
s*% is right ?

Looks reasonable.  Try it out.  I posted a simple example data step before. So use something like that to quickly test it on a lot of different strings.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

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
  • 6 replies
  • 550 views
  • 4 likes
  • 2 in conversation