I'm trying to use a "Like" operator on values within an array. Below are the data sets I have and want.
data have;
input code1 $ code2 $ code3 $;
datalines;
F334 F456 F213
F321 Y758 I893
F333 F231 F792
G567 F333 F334
F032 U234 F335
T430 R456 E345
;
run;
data want;
input code1 $ code2 $ code3 $ flag;
datalines;
F334 F456 F213 1
F321 Y758 I893 0
F333 F231 F792 1
G567 F333 F334 1
F032 U234 F335 1
T430 R456 E345 0
;
run;Below is my attempt and I believe gets at what I'm trying to do. Is there any way to do a like or wildcard operator on array operations like this?
data want;
set have;
array d code1-code3;
flag=0;
do i=1 to dim(d);
if d[i] like ( "F33%") then do;
flag=1;
leave;
end;
end;
drop i;
run;
I think that you may want to provide a bit of detail of exactly which you are counting "within an array".
If you want to compare if two strings begin with the same characters you can use the =: <look close there is a colon after the equal sign
data want;
set have;
array d code1-code3;
flag=0;
do i=1 to dim(d);
if d[i] =: "F33" then do;
flag=1;
leave;
end;
end;
drop i;
run;
I think that you may want to provide a bit of detail of exactly which you are counting "within an array".
If you want to compare if two strings begin with the same characters you can use the =: <look close there is a colon after the equal sign
data want;
set have;
array d code1-code3;
flag=0;
do i=1 to dim(d);
if d[i] =: "F33" then do;
flag=1;
leave;
end;
end;
drop i;
run;
I'm not well-versed on this technique, but some people in my department use the colon modifier within an array to do things like this. Someone correct me if this isn't a good method.
data want_2 (drop = i);
set have;
array f33_find [*] code:;
flag = 0;
do i = 1 to dim(f33_find);
if f33_find[i] in: ("F33") then flag = 1;
end;
run;
code1 code2 code3 flag F334 F456 F213 1 F321 Y758 I893 0 F333 F231 F792 1 G567 F333 F334 1 F032 U234 F335 1 T430 R456 E345 0
https://support.sas.com/resources/papers/proceedings/proceedings/sugi26/p073-26.pdf
Below another coding option.
data want;
input code1 $ code2 $ code3 $ flag;
flag_2= ( prxmatch('/\bF33/oi',catx(',',code1,code2,code3))>0 );
flag_3= ( find(','||catx(',',code1,code2,code3),',F33','i')>0 );
datalines;
F334 F456 F213 1
F321 Y758 I893 0
F333 F231 F792 1
G567 F333 F334 1
F032 U234 F335 1
T430 R456 E345 0
;
the "Colon Modifier" for the string comparisons in SAS is also known as "Bounded String Compare". Just to articulate the concepts so its easier search this forum.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.