DATA Step, Macro, Functions and more

PrxChange: avoid spaces in wildcard replacement

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 103
Accepted Solution

PrxChange: avoid spaces in wildcard replacement

[ Edited ]

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!

 

________________________

- That still only counts as one -


Accepted Solutions
Solution
‎03-07-2017 10:14 AM
Regular Contributor
Posts: 194

Re: PrxChange: avoid spaces in wildcard replacement

Hello,

Does the following replacement string answer your problem ?

/${1}0.$4/

View solution in original post


All Replies
Solution
‎03-07-2017 10:14 AM
Regular Contributor
Posts: 194

Re: PrxChange: avoid spaces in wildcard replacement

Hello,

Does the following replacement string answer your problem ?

/${1}0.$4/
Frequent Contributor
Posts: 103

Re: PrxChange: avoid spaces in wildcard replacement

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

________________________

- That still only counts as one -

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 151 views
  • 1 like
  • 2 in conversation