I am trying to match a phrase using the prxmatch function by passing it a column value.
data long_string;
infile datalines delimiter=',';
informat txt $40. keyp1 $20. keyp2 $20. keyp3 $20. keyp4 $20.;
input txt keyp1 keyp2 keyp3 keyp4;
datalines;
That is a great way to end the day,great way,end,today,even worse
;
run;
How can I go through the 4 keyp variables and match the phrase?
Well for one thing, the "o" pattern suffix instructs the matching function that the pattern is a constant, i.e. that it doesn't need to be recompiled at every invocation. Which is clearly not the case here.
BTW, the "i" suffix makes the matching case insensitive. So, no need for the lowcase() function call.
This seems to work:
data first_try;
set long_string;
match1 = prxmatch(catt("m/(",lowcase(keyp1),")/oi"),txt);
match2 = prxmatch(catt("m/(",lowcase(keyp2),")/oi"),txt);
match3 = prxmatch(catt("m/(",lowcase(keyp3),")/oi"),txt);
match4 = prxmatch(catt("m/(",lowcase(keyp4),")/oi"),txt);
run;
I don't quite understand why my array is not working:
data final;
set long_string;
array keyp{4} $ keyp1-keyp4;
array match{4} match1-match4;
do i=1 to 4;
match[i] = prxmatch(catt("m/(",lowcase(keyp[i]),")/oi"),txt);
end;
run;
Well for one thing, the "o" pattern suffix instructs the matching function that the pattern is a constant, i.e. that it doesn't need to be recompiled at every invocation. Which is clearly not the case here.
BTW, the "i" suffix makes the matching case insensitive. So, no need for the lowcase() function call.
That fixed it, thank you for insight.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.