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

I'm using prxmatch to match the string "END OF LIFE" but I don't want that string to match if it is followed by " CARE".  However, I can't seem to get the regular expression right (see below).  I could find lots of examples for excluding single characters but not for strings.

 

The regular expression should not match on "END OF LIFE CARE", but should match on "END OF LIFE MEDICATIONS" and "END OF LIFE OPTION" and "NEAR END OF LIFE OPTION"

 

data out;
set in;

if prxmatch('/(END OF LIFE[^( CARE)])/i',strip(string)) then output;
run;

 

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

HI @Ryanb2  Please try this-


data have;
input str $50.;
cards;
CARE
END OF LIFE CARE
END OF LIFE MEDICATIONS
END OF LIFE OPTION
NEAR END OF LIFE OPTION
END OF LIFE CARE jbjbj
jgjhbjhb
END OF LIFE
;
data want;
 set have;
 if prxmatch('/^END OF LIFE$|END OF LIFE\s\b(?:(?!CARE)\w)+\b/', strip(str));
run;

View solution in original post

7 REPLIES 7
novinosrin
Tourmaline | Level 20
data have;
input str $50.;
cards;
END OF LIFE CARE
END OF LIFE MEDICATIONS
END OF LIFE OPTION
NEAR END OF LIFE OPTION
;


data want;
 set have;
 if prxmatch('/END OF LIFE\s\b(?:(?!CARE)\w)+\b/', str);
run;
Ryanb2
Quartz | Level 8

Yes!  That works for the criteria I listed.  I forgot to include one more though, "END OF LIFE" by itself without any characters or space before or after.  This should match as well but doesn't using the regular expression you wrote.  Can this be modified to include this as well?

novinosrin
Tourmaline | Level 20

HI @Ryanb2  Please try this-


data have;
input str $50.;
cards;
CARE
END OF LIFE CARE
END OF LIFE MEDICATIONS
END OF LIFE OPTION
NEAR END OF LIFE OPTION
END OF LIFE CARE jbjbj
jgjhbjhb
END OF LIFE
;
data want;
 set have;
 if prxmatch('/^END OF LIFE$|END OF LIFE\s\b(?:(?!CARE)\w)+\b/', strip(str));
run;
Ryanb2
Quartz | Level 8

That worked!  Thank you.

 

It's frustrating the solution is not simpler.  It seems logical and practical that strings to exclude could be contained in parentheses like in my example code.  It would make it a lot easier to add more strings to exclude if testing showed more were needed.

 

Much appreciated. 

novinosrin
Tourmaline | Level 20

Hi @Ryanb2  Personally, I am glad SAS (my only religion) is rather vast and hopefully(complex) for I would always have something new to learn. Believing I have some real youth left and can have reasonably good mental and physical health(including my immediate family) with reasonable economic conditions over the next 25+ years, I would love and want to master ins and outs of SAS in its entirety.  Peace!

LeonidBatkhan
Lapis Lazuli | Level 10

Hi

 

You can use find() function to achieve this:

data HAVE;
   infile datalines truncover;
   input STRING $500.;
   datalines;
END OF LIFE CARE
END OF LIFE MEDICATIONS
END OF LIFE OPTION
NEAR END OF LIFE OPTION
SOMETHING ELSE
;

data WANT;
   set HAVE;
   if find(string,'END OF LIFE') and not find(string,'END OF LIFE CARE');
run;

Hope this helps.

Ryanb2
Quartz | Level 8

Thanks for your response.  I considered doing something like this, but I simplified my code greatly in this post.  I need to match on far more strings and for many more fields in a SQL statement.  I will likely need to exclude more strings as well.  If I was working in a data step I might write something like this and then process all the columns in an array, but I'm looking for a regular expression solution.

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
  • 7 replies
  • 1876 views
  • 5 likes
  • 3 in conversation