turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- General Programming
- /
- Combinations of binary indicator

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-10-2017 03:09 PM

Hi everyone,

I would like to generate all possible combinations of three variables each with values 0 or 1 e.g. following using a data step

000

001

010

100

011

101

110

111

what will be a good way to do it?

Accepted Solutions

Solution

04-10-2017
09:37 PM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to kaushik_patra

04-10-2017 09:22 PM - edited 04-10-2017 11:22 PM

So maybe something like this.

```
data want;
array p[2] _temporary_ (0 1);
array x[3];
do _n_=0 to dim(p)**dim(x)-1;
do i=1 to dim(x);
x(i)=p(1+mod(int(_n_/(dim(p)**(i-1))),dim(p))) ;
end;
output;
end;
drop i ;
run;
```

All Replies

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to kaushik_patra

04-10-2017 03:21 PM

Check out the ALLCOMB function in SAS. Designed just for such a thing!

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ChrisHemedinger

04-10-2017 06:50 PM

Thanks, it's a nice function. But the problem is I have n=2 (0 or 1) but k=3 which doesn't work for ALLCOMB.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to kaushik_patra

04-10-2017 07:06 PM

A not terribly bright solution

data _null_; do i='0','1'; do j='0','1'; do k='0','1'; comb=cats(i,j,k); put comb=; end; end; end; run;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ballardw

04-10-2017 07:27 PM

Thanks. The need is to have three distinct columns rather than a single column with all possible combinations.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to kaushik_patra

04-10-2017 03:36 PM

These combinations are just a sequence of binary numbers. You don't need any data to do that:

data want;

do i=0 to 7;

result = put(i, binary3.);

output;

end;

drop i;

run;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Astounding

04-10-2017 07:03 PM

Thanks, this is a nice little trick. I will have to have 3 different columns for all combinations of 0 or 1.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to kaushik_patra

04-10-2017 07:35 PM

For three it is simple.

```
data want;
do a=0,1; do b=0,1; do c=0,1;
output;
end; end; end;
run;
```

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-10-2017 07:49 PM

Great! This has promise. If I have two arrays

P = (0 1) and x = (x1 x2 x3) where each of x takes values 0 or 1, how would you do it?

P = (0 1) and x = (x1 x2 x3) where each of x takes values 0 or 1, how would you do it?

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to kaushik_patra

04-10-2017 08:07 PM

Not sure what you are talking about at this point. If you have four variables named P, X1 , X2, and X3 then you could build all combinations using similar DO loops. If you have two tables and want to join them to produce all possible combinations then PROC SQL does a good job.

```
proc sql ;
create table want as
select *
from P, X
;
quit;
```

If you really have two vectors and what to manipulate them then use PROC IML. You can do lots of matrix manipulation using a language designed to work with matrices.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-10-2017 08:18 PM

Thanks, I have the following data -

Data a;

Array p[2] (0 1);

Array x[3];

I would like the data step operations to produce 8 rows with the unique combinations.

Data a;

Array p[2] (0 1);

Array x[3];

I would like the data step operations to produce 8 rows with the unique combinations.

Solution

04-10-2017
09:37 PM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to kaushik_patra

04-10-2017 09:22 PM - edited 04-10-2017 11:22 PM

So maybe something like this.

```
data want;
array p[2] _temporary_ (0 1);
array x[3];
do _n_=0 to dim(p)**dim(x)-1;
do i=1 to dim(x);
x(i)=p(1+mod(int(_n_/(dim(p)**(i-1))),dim(p))) ;
end;
output;
end;
drop i ;
run;
```

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-10-2017 09:37 PM

This is cool. I have devised a similar solution (but less technical) based on Astounding's proposal.

data a;

array p[2] _temporary_;

array x[3];

do i=1 to dim(p)**dim(x);

result = put(i, binary3.);

do j=1 to 3;

x[j]=substr(result,j,1);

end;

output;

end;

drop i j;

run;

data a;

array p[2] _temporary_;

array x[3];

do i=1 to dim(p)**dim(x);

result = put(i, binary3.);

do j=1 to 3;

x[j]=substr(result,j,1);

end;

output;

end;

drop i j;

run;