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

Hi,

 

I hope you are doing fine, I am looking for a trick in a regular expression statement.

 

Question:Is there a direct way to avoid placing spaces in a 'prxchange replacement part' between a wildcard and a zero?

Example:/$1 0.$4/ (use group1, followed by a zero, followed by a dot, followed by the fourth group)?

 

Problem: I do not want any space between '$1' and my '0' but if I write /$10.$4/ SAS wants to replace with the 10th group, followed by a dot, followed by the fourth group...

 

Background: I am using a regex to ensure that my lab values have a leading zero if my string looks like a number with decimals (even when preceeded by a logical operator).

 

data test;
length a expect $40;

a='.064';      expect='0.064';      output;
a='.064   ';   expect='0.064';      output;
a='   .064   ';expect='0.064';      output;
a='<.01';      expect='<0.01';      output;
a='1.1';       expect='1.1';        output;
a='.064zzz';   expect='.064zzz';    output;
a='0';         expect='0';          output;
a='99.6';      expect='99.6';       output;
a='';          expect='';           output;
a='<3.17';     expect='<3.17';      output;
a='<3.17zzz';  expect='<3.17zzz';   output;
a='<=.17';     expect='<=0.17';     output;
a='>=.17';     expect='>=0.17';     output;

a='   <=.17';  expect='<=0.17';     output;
a='   >=.17';  expect='>=0.17';     output;

a='<>.17';     expect='<>0.17';     output;
a='<=>.17';    expect='<=>.17';    output;
a='<>.17zzz';  expect='<>.17zzz';   output;
a='.064 .064'; expect='.064 .064';  output;
a='>60';       expect='>60';        output;
run;
data test1; set test; length b $40; b=a; if prxmatch('/^\s*((<|>|=){0,2})\s*(\.)(\d+)\s*$/',a) then do; b=strip(tranwrd(a,'.','0.')); end; c=prxchange('s/^\s*((<|>|=){0,2})\s*(\.)(\d+)\s*$/$1 0.$4/',1,a); c1=prxchange('s/^\s*((<|>|=){0,2})\s*(\.)(\d+)\s*$/$1/',1,a); c2=prxchange('s/^\s*((<|>|=){0,2})\s*(\.)(\d+)\s*$/$2/',1,a); c3=prxchange('s/^\s*((<|>|=){0,2})\s*(\.)(\d+)\s*$/$3/',1,a); c4=prxchange('s/^\s*((<|>|=){0,2})\s*(\.)(\d+)\s*$/$4/',1,a); if b ne trim(expect) then FAILb='YES'; if c ne trim(expect) then FAILc='YES'; run; /* '^ Start of the string \s* any number of spaces ((<|>|=){0,2}) a combination of maximal 2 operators with <,> or = (First group) \s* any number of spaces (\.) a dot (Third group) (\d+) at least one digit (Fourth group) \s* any number of spaces $ End of the string */

I would like c to get the same result as b

Thanks!

 

________________________

- Cheers -

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12
Hello,

Does the following replacement string answer your problem ?

/${1}0.$4/

View solution in original post

2 REPLIES 2
gamotte
Rhodochrosite | Level 12
Hello,

Does the following replacement string answer your problem ?

/${1}0.$4/
Oligolas
Barite | Level 11

Excellent, I didn't think of placing the group number in curlys. Thanks!

________________________

- Cheers -

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2 replies
  • 1885 views
  • 1 like
  • 2 in conversation