Solved
New Contributor
Posts: 4

# 2^N Matrix.

¡Hi everybody!

I am trying to get a matrix with all the possibles combinations of "0 and 1" i have seen the function "IML" but i not have access to that function,would it be possible por me program all that?

i am with this one:

/*just create three data sets (all hold 0,1)*/
data a;
input x@@;
datalines;
0 1
;

data b;
input y@@;
datalines;
0 1
;

data c;
input z@@;
datalines;
0 1
;

/*use Cartesian product to get all the 2*2*2 combinations*/
proc sql;
create table all as
select *
from a,b,c
order by x,y,z
;
quit;

proc print nobs;run; but what if i want to get into a lot of more data? i mean maximum 1555030x1555030 with all the combinations (0 and 1) possibles, sql doest not allowed me to do that.

Thank you so much for your help.

I am trying to generate a loss probability distribution.

Accepted Solutions
Solution
‎05-18-2017 10:11 AM
Frequent Contributor
Posts: 147

## Re: 2^N Matrix.

There are several ways I can think of to get the matrix you want.  For example PROC PLAN will produce a matrix of 1s and 2s :

``````proc plan;
factors x=2 ordered y=2 ordered z=2 ordered;
output out=all;
run;``````

Alternatively consider GRAYCODE which you can use like this:

``````data all (drop = i k);
array x(3);
k = -1;
do i = 1 to 2**3;
call graycode(k, of x(*) );
output;
end;
run;``````

Having a loop is an advantage, as you could add an IF statement to only output the possibilities that you are interested in.

However, you will never be able to process 2 ^ 1,555,043 possibilites as this number is far too big!  Even 2 ^ 30 is pushing against limits, as this is above 1 billion.

I think you will need a more sophisticated approach, but you will need to give more details on what you want to achieve, to get any useful help.   If you do not have IML, then it would be better to ask this question in a different forum.

All Replies
Super User
Posts: 19,868

## Re: 2^N Matrix.

This isn't that clear.

Are you required to use IML? If you only have two values why do you have so many options?

I think you need to further explain your issue.

You can look at the documentation for ALLCOMB or CALL ALLCOMB on other ways to create all possible combinations of data.

New Contributor
Posts: 4

## Re: 2^N Matrix.

Thank you so much for your time!!

Yeaap,sorry i was not that clear,see i am an actuarie working for a BANK, i am trying to do this:

Suppose i have this:

 Portfolio Default probability exposure 0.06 1,000,000.00 0.05 780,000.00 0.04 500,000.00 0.02 250,000.00 0.01 100,000.00

then i am creating all the possibles combinations for that 5 number of persons with just a flag(1 or 0) ,with 1 (default) and 0(no default)  then i have the combinations: 2^5 i mean 2^n., generating this:

 Case X1 X2 X3 X4 X5 1 1 1 1 1 1 2 1 1 1 1 0 3 1 1 1 0 1 4 1 1 1 0 0 5 1 1 0 1 1 6 1 1 0 1 0 7 1 1 0 0 1 8 1 1 0 0 0 9 1 0 1 1 1 10 1 0 1 1 0 11 1 0 1 0 1 12 1 0 1 0 0 13 1 0 0 1 1 14 1 0 0 1 0 15 1 0 0 0 1 16 1 0 0 0 0 17 0 1 1 1 1 18 0 1 1 1 0 19 0 1 1 0 1 20 0 1 1 0 0 21 0 1 0 1 1 22 0 1 0 1 0 23 0 1 0 0 1 24 0 1 0 0 0 25 0 0 1 1 1 26 0 0 1 1 0 27 0 0 1 0 1 28 0 0 1 0 0 29 0 0 0 1 1 30 0 0 0 1 0 31 0 0 0 0 1 32 0 0 0 0 0

i have read about iml but i don´t have such a function,i am nor advanced at macros but the previous code i left seems to me to have the logic, it is just that i do not know how to construct this matriz since i don't have iml. My maximum number of clients is 1,555,043 and i am expecting tgis matrix to be 2^1,555,043 in order to get all the cambinations and finally get mi distribution.

Thank you so so much for your time.

Best regards.

Fátima.

PROC Star
Posts: 765

## Re: 2^N Matrix.

I'm still confused. Do you want to use IML or another SAS procedure to do this?

New Contributor
Posts: 4

## Re: 2^N Matrix.

Hi,

No IML,I need another procedure.

New Contributor
Posts: 4

## Re: 2^N Matrix.

Hi,

No IML,I need another procedure.

Thank you.

Super User
Posts: 19,868

## Re: 2^N Matrix.

You posted this in IML.

I'm not sure what you want is feasible. It's possible but I wonder if a different structure wouldn't work better. Can you explain what you will do with the matrix after it's created.

Solution
‎05-18-2017 10:11 AM
Frequent Contributor
Posts: 147

## Re: 2^N Matrix.

There are several ways I can think of to get the matrix you want.  For example PROC PLAN will produce a matrix of 1s and 2s :

``````proc plan;
factors x=2 ordered y=2 ordered z=2 ordered;
output out=all;
run;``````

Alternatively consider GRAYCODE which you can use like this:

``````data all (drop = i k);
array x(3);
k = -1;
do i = 1 to 2**3;
call graycode(k, of x(*) );
output;
end;
run;``````

Having a loop is an advantage, as you could add an IF statement to only output the possibilities that you are interested in.

However, you will never be able to process 2 ^ 1,555,043 possibilites as this number is far too big!  Even 2 ^ 30 is pushing against limits, as this is above 1 billion.

I think you will need a more sophisticated approach, but you will need to give more details on what you want to achieve, to get any useful help.   If you do not have IML, then it would be better to ask this question in a different forum.

Super User
Posts: 11,343

## Re: 2^N Matrix.

I see absolutely no connection between your "Portfolio" information and the example desired output;

This macro makes a data set named Want with the desired output though the 0 values come first. If you don't like that change the

do x&i=0 to 1 into do x&i = 1 to 0 by (-1);

```%macro dummy(numPers);

data want;
retain case 0;
%do i=1 %to &numpers;
do x&i = 0 to 1;
%end;
case +1;
output;
%do i=1 %to &numpers;
end;
%end;
run;
%mend;

%dummy(5);```
Super User
Posts: 10,044

## Re: 2^N Matrix.

```
%let n=5;
%let x=%substr(%qsysfunc(repeat(%str(,0:1),&n)),2);

%put &x ;

proc iml;
want=expandgrid(&x);
print want;
quit;

```
Super User
Posts: 19,868

## Re: 2^N Matrix.

I moved this post to Base SAS forum.

☑ This topic is solved.