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;
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
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
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 -
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
Thank you so much for all of your kind help.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.