Help using Base SAS procedures

Prxmatch condition?

Accepted Solution Solved
Reply
Super Contributor
Posts: 319
Accepted Solution

Prxmatch condition?

Hello:

 

I use 'Prxmatch' to search the strings below.   I found the 'I' is for 'or' condition.  Is there a way could do in the 'and' condition?

 

data datain;

format name $ 500.;

input name &;

cards;

John__If_True_kary

John_If_True__Mary

John__If_True1_kary

John_If_True_kary

Tom__If_Not__Carol

Tom__If_Not_Carol

Tom_If_Not1_Carol

Joe__If_True___Jane

Joe__If_False_Jane

Joe__If_False__Paul1

Joe__If_False___Jane

Paul_If_False2__Jane

Joe__If_False2___Jane

Joe___If_False__Jane

;

run;

 

data dataout;

      set datain;

      if prxmatch("m/If_True|mary/i",name) > 0 then found=1;

      else if prxmatch("m/If_True|kary/i",name) > 0 then found=2;

      else if prxmatch("m/If_False|Jane/i",name) > 0 then found=3;

      else if prxmatch("m/If_False|paul/i",name) > 0 then found=4;

      else if prxmatch("m/If_Not|Carol/i",name) > 0 then found=5;

else found=0;

run;

quit;

 

 

The result I am looking for is

 

if index(lowcase(name),' If_True ') > 0 and  index(lowcase(name),' mary ') > 0 and  index(lowcase(name),' John ') > 0 then found = 1;

   else found=0;

 

 

 


Accepted Solutions
Solution
‎06-26-2017 10:36 AM
PROC Star
Posts: 7,492

Re: Prxmatch condition?

If the order of the values isn't relevant you could use something like:

data dataout;
  set datain;
  if prxmatch("m/(?=.*?If_True)(?=.*?mary)/i",name) > 0 then found=1;
  else if prxmatch("m/(?=.*?If_True)(?=.*?kary)/i",name) > 0 then found=2;
  else if prxmatch("m/(?=.*?If_False)(?=.*?Jane)/i",name) > 0 then found=3;
  else if prxmatch("m/(?=.*?If_False)(?=.*?paul)/i",name) > 0 then found=4;
  else if prxmatch("m/(?=.*?If_Not)(?=.*?Carol)/i",name) > 0 then found=5;
  else found=0;
run;

Art, CEO, AnalystFinder.com

View solution in original post


All Replies
PROC Star
Posts: 326

Re: Prxmatch condition?

[ Edited ]

something like this

data dataout;

      set datain;

      if prxmatch("m/^John.*If_True.*mary$/i",trim(name)) > 0 then found=1;

      else found = 0;

run;

quit;

if john is no exactly at begining then use .* instead of ^ and if mary is not exactly at the end then replace $ with .* . Please remmeber order of values is important

 

Frequent Contributor
Posts: 129

Re: Prxmatch condition?

Hi,

 

I am a passionate user and defender of regular expressions but I think you are missusing them by using them in this context and increasing your program complexity without any gain.

I think you would be better advised to split the 'name' string into several variables and adapt your logic in accordance. It would be much easier for you to review and debug.

 

DATA Datain2;
   set datain;
   length firstname1 condition firstname2 $200;
   firstname1=scan(name,1,'_');
   condition=scan(name,2,'_')||'_'||scan(name,3,'_'); 
   firstname2=scan(name,4,'_'); 

   if firstname1 eq '...' AND/OR .... then found=...;
   else if ....;
RUN;
________________________

- Cheers -

Solution
‎06-26-2017 10:36 AM
PROC Star
Posts: 7,492

Re: Prxmatch condition?

If the order of the values isn't relevant you could use something like:

data dataout;
  set datain;
  if prxmatch("m/(?=.*?If_True)(?=.*?mary)/i",name) > 0 then found=1;
  else if prxmatch("m/(?=.*?If_True)(?=.*?kary)/i",name) > 0 then found=2;
  else if prxmatch("m/(?=.*?If_False)(?=.*?Jane)/i",name) > 0 then found=3;
  else if prxmatch("m/(?=.*?If_False)(?=.*?paul)/i",name) > 0 then found=4;
  else if prxmatch("m/(?=.*?If_Not)(?=.*?Carol)/i",name) > 0 then found=5;
  else found=0;
run;

Art, CEO, AnalystFinder.com

Super Contributor
Posts: 319

Re: Prxmatch condition?

Thank you so much for all of your kind help.

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 208 views
  • 3 likes
  • 4 in conversation