BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
FatimaSS
Fluorite | Level 6

¡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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
IanWakeling
Barite | Level 11

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

10 REPLIES 10
Reeza
Super User

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.

FatimaSS
Fluorite | Level 6

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.

PeterClemmensen
Tourmaline | Level 20

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

FatimaSS
Fluorite | Level 6

Hi,

No IML,I need another procedure.

 

FatimaSS
Fluorite | Level 6

Hi,

No IML,I need another procedure.

 Thank you.

Reeza
Super User

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. 

IanWakeling
Barite | Level 11

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.

ballardw
Super User

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);
Ksharp
Super User

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

%put &x ;

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



Reeza
Super User

I moved this post to Base SAS forum. 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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