Hello,
Is there a way to simplify the codes below. I have around 15 lines of Prxmatch. I list a few of them below, thanks.
data want; set have; if prxmatch('/uairway/i',uothersp) then uairway=1; if prxmatch('/uapnea/i',uothersp) then uapnea=1; if prxmatch('/ugastro/i',uothersp) then ugastro=1; if prxmatch('/ugerd/i',uothersp) then ugerd=1; if prxmatch('/uendo/i',uothersp) then uendo=1; if prxmatch('/uallergy/i',uothersp) then uallergy=1; if prxmatch('/uprem/i',uothersp) then uprem=1; if prxmatch('/ugestage/i',uothersp) then ugestage=1; run;
Hello,
Following @Reeza advice of using arrays :
data want;
set have;
array vars uairway uapnea ugastro ugerd uendo uallergy uprem ugestage;
do over vars;
if find(uothersp,vname(vars),"i") then vars=1;
end;
run;
Can there be many matches for the same string?
Not a simplification (I don't think you can in a meaningful way), but a performance improvement: The find() function is a lot less expensive than the prxmatch() function.
Also this syntax might be slightly more compact:
UAIRWAY = find(UOTHERSP, 'uairway', 'i') > 0 ;
If you really want to remove the tests, you can do something like this:
%let strings=UAIRWAY UGASTRO;
%macro loop;
%local i;
%do i=1 %to %sysfunc(countw(&strings));
%scan(&strings,&i) = find(UOTHERSP, "%scan(&strings,&i)", 'i') > 0 ;
%end;
%mend;
%loop;
You removed 15-8=7 lines of code, but it's a lot less legible now. Not worth it imho.
Hello,
Following @Reeza advice of using arrays :
data want;
set have;
array vars uairway uapnea ugastro ugerd uendo uallergy uprem ugestage;
do over vars;
if find(uothersp,vname(vars),"i") then vars=1;
end;
run;
Thank you so much, ChrisNZ and Gamotte. Both programs work!
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.