## Using Arrays to check equality of groups of strings

Solved
Occasional Contributor
Posts: 5

# Using Arrays to check equality of groups of strings

Hi,

By way of example, I have two groups of strings as shown below, a1-a3 and b1-b3.

I want to know if at least one of the strings in a1-a3 is equal to at least one of the strings in b1-b3. Then flag the rows which satisfy this.

I imagine this will be done using arrays and a do loop. I realise my attempt in the code below will only work if all a1-a3 are identical to b1-b3.

I would really appreciate some help here.

Thanks a lot

Matt

``````data table ;
input a1 \$ a2 \$ a3 \$ b1 \$ b2 \$ b3 \$ match \$ ;
cards ;
a b c a b c Yes
a b c a d e Yes
a b c d e f no
a b c a b e Yes
;
run ;

data table ;
set table ;
array check1 a1-a3 ;
array check2 b1-b3 ;
do i=1 to 3 ;
if check1(i)=check2(i) then match =1 ;
else match=0 ;
end ;
run ;``````

Accepted Solutions
Solution
‎04-11-2018 06:12 AM
Super User
Posts: 9,840

## Re: Using Arrays to check equality of groups of strings

Something like:

```data table ;
input a1 \$ a2 \$ a3 \$ b1 \$ b2 \$ b3 \$;
cards ;
a b c a b c
a b c a d e
a b c d e f
a b c a b e
;
run;

data want;
set table;
array a{3};
do i=1 to dim(a);
if whichc(a{i},of b:) then match="yes";
end;
run;```

All Replies
Super User
Posts: 10,571

## Re: Using Arrays to check equality of groups of strings

Just a little change in the logic:

``````data table;
set table;
array check1 a1-a3;
array check2 b1-b3;
match = 0;
do i = 1 to 3;
if check1{i} = check2{i} then match = 1;
end;
run;``````
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 5

## Re: Using Arrays to check equality of groups of strings

Thank you so much pal

Solution
‎04-11-2018 06:12 AM
Super User
Posts: 9,840

## Re: Using Arrays to check equality of groups of strings

Something like:

```data table ;
input a1 \$ a2 \$ a3 \$ b1 \$ b2 \$ b3 \$;
cards ;
a b c a b c
a b c a d e
a b c d e f
a b c a b e
;
run;

data want;
set table;
array a{3};
do i=1 to dim(a);
if whichc(a{i},of b:) then match="yes";
end;
run;```

☑ This topic is solved.