Solved
Contributor
Posts: 28

# Get the combinations with do loop

Say we have 4 letters--"A", "B","C","D". Now I want to get the combinations, the result should be 16 ELEMENTS.

AA
AB
AC
BA
BB
BC
BD
CA
CB
CC
CD
DA
DB
DC
DD

Thank you.

Accepted Solutions
Solution
‎03-14-2017 08:22 PM
PROC Star
Posts: 8,169

## Re: Get the combinations with do loop

Not sure what you're asking for. Is it something like:

data want (keep=first: second:);
array first(16) \$;
array second(16) \$;
do i = 'A', 'B', 'C', 'D';
do j = 'A', 'B', 'C', 'D';
cell+1;
first(cell)=i;
second(cell)=j ;
end;
end;
run;

Art, CEO, AnalystFinder.com

All Replies
PROC Star
Posts: 311

## Re: Get the combinations with do loop

data want;
do i = 'A', 'B', 'C', 'D';
do j = 'A', 'B', 'C', 'D';
var = cats(i, j);
output;
end;
end;
run;
Contributor
Posts: 28

## Re: Get the combinations with do loop

Hi,

I want the result can be an arrary format, so I can retrieve the first element and second item etc.

So for example, for AA, it is an array. The first item of the array is A, the second item of array is A also.

PROC Star
Posts: 311

## Re: Get the combinations with do loop

I'm really sorry, but I don't understand. The code I provided gives you the results you specified. The i and j give you the two separate portions, if that's what you mean.

Solution
‎03-14-2017 08:22 PM
PROC Star
Posts: 8,169

## Re: Get the combinations with do loop

Not sure what you're asking for. Is it something like:

data want (keep=first: second:);
array first(16) \$;
array second(16) \$;
do i = 'A', 'B', 'C', 'D';
do j = 'A', 'B', 'C', 'D';
cell+1;
first(cell)=i;
second(cell)=j ;
end;
end;
run;

Art, CEO, AnalystFinder.com

Super User
Posts: 13,583

## Re: Get the combinations with do loop

sas_newbie3 wrote:

Hi,

I want the result can be an arrary format, so I can retrieve the first element and second item etc.

So for example, for AA, it is an array. The first item of the array is A, the second item of array is A also.

Then go back to your orginal post and change the "example" data to what you actually want. Your example implied one record per two character concatenated string.

And your description would result in 32 values not 16:

A,A,A,B,A,C,A,D,B,A,B,B,B,C,B,D,C,A,C,B,C,C,C,D,D,A,D,B,D,C,D,D

data junk;
array x {32} \$1.;
counter=0;
do i= 'A','B','C','D';
do j= 'A','B','C','D';
counter+1;
x[counter]=i ;
counter+1;
x[counter]=j ;
end;
end;
output;
drop counter i j;
run;
☑ This topic is solved.