BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SASdevAnneMarie
Barite | Level 11

Hello Experts,

 

With prxmath function I would like to select only the values 2,25 or 2.25, 1.75 or 1,75.

So, in my code I have :  if prxmatch("m/2,25|2.25|1,75|1.75/oi",lb)>0 , but I don't know why this doesn't work, beacause I have in my selection 2.50.

 

Thank your for your help !

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @SASdevAnneMarie,

 

The issue is that the period (.) is a metacharacter that "matches any single character except newline" (see table "Other Quantifiers" in the documentation). But if you "escape" it with a backslash (see table "General Constructs"), it matches the period "." as intended. I would also suggest using the "word boundary" metacharacter \b to avoid matches of "12,25", "41.75" etc.:

"/\b2,25|\b2\.25|\b1,75|\b1\.75/"

But, of course, it depends on your data whether this makes sense or not (e.g., if you want to match the "2.25" in "Q2.25").

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

Explain in more detail. Show us the code. Show us a portion of the data.

 

By the way, you should always show us your code that doesn't work. We have asked for this  from you before. Do not make us ask repeatedly. 

--
Paige Miller
FreelanceReinh
Jade | Level 19

Hello @SASdevAnneMarie,

 

The issue is that the period (.) is a metacharacter that "matches any single character except newline" (see table "Other Quantifiers" in the documentation). But if you "escape" it with a backslash (see table "General Constructs"), it matches the period "." as intended. I would also suggest using the "word boundary" metacharacter \b to avoid matches of "12,25", "41.75" etc.:

"/\b2,25|\b2\.25|\b1,75|\b1\.75/"

But, of course, it depends on your data whether this makes sense or not (e.g., if you want to match the "2.25" in "Q2.25").

SASdevAnneMarie
Barite | Level 11
Thank you ! That works ! Do you know please how to add the condition and in this expression, like "/\b2,25|\b2\.25|\b1,75|\b1\.75/ and 'RATE'".
FreelanceReinh
Jade | Level 19

@SASdevAnneMarie wrote:
Do you know please how to add the condition and in this expression, like "/\b2,25|\b2\.25|\b1,75|\b1\.75/ and 'RATE'".

So you want to check if a string contains one (or more) of the numbers discussed previously and also the string "RATE" (or "'RATE'" with single quotes? Perhaps case-insensitive?)?

 

You could search for "RATE" with a separate function call (e.g., of the FINDW function). However, if you know positively that "RATE" would always occur before (or always after) the number, you can add it conveniently to the regular expression:

"/\bRATE\b.*(\b2,25|\b2\.25|\b1,75|\b1\.75)/i"

This regex matches RATE, rate, Rate, etc. (but not Strategy, rated, etc.), followed by a word boundary and a sequence of (zero or more) virtually arbitrary characters and then any of the four numbers of interest. So, example strings matching this pattern are

Rate:1,75
The rate is exactly 2.25%.
estimated rate: 1.737 (95%-CI: 1.716-1.758)

The latter match could be avoided by adding \b metacharacters after the numbers, but then 2.250 etc. would no longer match (without further extensions of the regex).

SASdevAnneMarie
Barite | Level 11
Thank you very much !

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 339 views
  • 5 likes
  • 3 in conversation