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

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;

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

4 REPLIES 4
kiranv_
Rhodochrosite | Level 12

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

 

Oligolas
Barite | Level 11

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 -

art297
Opal | Level 21

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

ybz12003
Rhodochrosite | Level 12

Thank you so much for all of your kind help.

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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