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 -
Excellent, I didn't think of placing the group number in curlys. Thanks!
- Cheers -
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.