DATA Step, Macro, Functions and more

2^N Matrix.

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

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: 137

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.

View solution in original post


All Replies
Super User
Posts: 17,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 probabilityexposure
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:

CaseX1X2X3X4X5
111111
211110
311101
411100
511011
611010
711001
811000
910111
1010110
1110101
1210100
1310011
1410010
1510001
1610000
1701111
1801110
1901101
2001100
2101011
2201010
2301001
2401000
2500111
2600110
2700101
2800100
2900011
3000010
3100001
3200000

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: 552

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: 17,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: 137

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: 10,516

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: 9,682

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: 17,868

Re: 2^N Matrix.

I moved this post to Base SAS forum. 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 335 views
  • 8 likes
  • 6 in conversation