Hello,
I use prematch function below. It seems like my program wasn't working. How to fix it? Thanks.
data datain;
format name $ 500.;
input name &;
cards;
CAR ID 5648MA
0009463
000-4721
;
run;
data dataout;
set datain;
if prxmatch("m/(?=.*?If_True)(?=.*?5648)/i",name) > 0 then found=1;
else if prxmatch("m/(?=.*?If_True)(?=.*?9463)/i",name) > 0 then found=2;
else if prxmatch("m/(?=.*?If_False)(?=.*?4721)/i",name) > 0 then found=3;
else found=0;
run;
Looks like you based your code on another Communities answer where If_True and If_False wereactually part of the data. They are not part of any regex syntax. Here they are not so you will always see found=0. I have simplified your regex and this works for me. I hope it also work for you.
data dataout;
set datain;
if prxmatch("m/.*?5648/i",name) > 0 then found=1;
else if prxmatch("m/.*?9463/i",name) > 0 then found=2;
else if prxmatch("m/.*?4721/i",name) > 0 then found=3;
else found=0;
run;
Regards,
- Jan.
Looks like you based your code on another Communities answer where If_True and If_False wereactually part of the data. They are not part of any regex syntax. Here they are not so you will always see found=0. I have simplified your regex and this works for me. I hope it also work for you.
data dataout;
set datain;
if prxmatch("m/.*?5648/i",name) > 0 then found=1;
else if prxmatch("m/.*?9463/i",name) > 0 then found=2;
else if prxmatch("m/.*?4721/i",name) > 0 then found=3;
else found=0;
run;
Regards,
- Jan.
Hi @ybz12003
A simplier approach, assuming that the condition to flag the variable is to find the pattern anywhere in the string (no condition such as the string ends with the pattern, ...)
data dataout;
set datain;
if prxmatch("/5648/",name) > 0 then found=1;
else if prxmatch("/9463/",name) > 0 then found=2;
else if prxmatch("/4721/",name) > 0 then found=3;
else found=0;
run;
-> no need to add the "i" modifier as you look for digits and not letters
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.