BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
A_SAS_Man
Pyrite | Level 9

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

Accepted Solutions
ballardw
Super User

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;

View solution in original post

4 REPLIES 4
ballardw
Super User

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;
maguiremq
SAS Super FREQ

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 

Patrick
Opal | Level 21

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

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.

sas-innovate-wordmark-gradient-background 3.pngSAS Innovate

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

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
  • 4 replies
  • 3348 views
  • 5 likes
  • 5 in conversation