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,

 

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;
1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

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;

View solution in original post

6 REPLIES 6
Reeza
Super User
Have you tried an array?
PGStats
Opal | Level 21

Can there be many matches for the same string?

PG
ChrisNZ
Tourmaline | Level 20

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 ; 
ChrisNZ
Tourmaline | Level 20

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.

 

gamotte
Rhodochrosite | Level 12

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;
ybz12003
Rhodochrosite | Level 12

Thank you so much, ChrisNZ and Gamotte.   Both programs work!

sas-innovate-wordmark-2025-midnight.png

Register Today!

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.


Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1009 views
  • 8 likes
  • 5 in conversation