<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How can I find the Combinations of factor levels in SAS/IML in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627509#M4989</link>
    <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;

start FullFactorial( n );
  r = 2 ## n;
  x = j(r, n);
  pot = 2 ## ( 0:(n-1) );  /* reversed powers of 2 in this line */
  do i = 1 to r;
    x[i, ] = band( i-1, pot ) &amp;gt; 0;
  end;
  return( x );
finish;

a = FullFactorial( 4 );
print a [colname={A B C D}];
 
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 26 Feb 2020 14:51:10 GMT</pubDate>
    <dc:creator>IanWakeling</dc:creator>
    <dc:date>2020-02-26T14:51:10Z</dc:date>
    <item>
      <title>How can I find the Combinations of factor levels in SAS/IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627484#M4978</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am working on SAS/IML. I am trying to figure out the number of combinations of factors in factorial design as follows :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;imagine we have 4 factors, A, B, C and D. I want to get the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;A&lt;/P&gt;&lt;P&gt;B&lt;/P&gt;&lt;P&gt;AB&lt;/P&gt;&lt;P&gt;C&lt;/P&gt;&lt;P&gt;AC&lt;/P&gt;&lt;P&gt;BC&lt;/P&gt;&lt;P&gt;ABC&lt;/P&gt;&lt;P&gt;D&lt;/P&gt;&lt;P&gt;AD&lt;/P&gt;&lt;P&gt;BD&lt;/P&gt;&lt;P&gt;ABD&lt;/P&gt;&lt;P&gt;CD&lt;/P&gt;&lt;P&gt;ACD&lt;/P&gt;&lt;P&gt;BCD&lt;/P&gt;&lt;P&gt;ABCD&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basically the first four combinations (1, A, B, AB) are always the same for any number of factors (m) so I have put this in a matrix form as initial combinations:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ncomb = 2**(m);&lt;BR /&gt;print ncomb;&lt;BR /&gt;initial_combs = j(4,m,0);&lt;BR /&gt;initial_combs[2,1] = 1;&lt;BR /&gt;initial_combs[3,2] = 1;&lt;BR /&gt;initial_combs[4,1] = 1;&lt;BR /&gt;initial_combs[4,2] = 1;&lt;BR /&gt;print initial_combs;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have used 0's and 1's to indicate the absence and presence of the factor respectively.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The I used the initial_comb matrix to add the following four combinations (C, AC, BC, ABC). All I have done is adding C to the initial combinations:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;combs = j(4,m,0);&lt;BR /&gt;count = 3;&lt;BR /&gt;do i = 1 to 4;&lt;BR /&gt;do j = 1 to m;&lt;BR /&gt;if initial_combs[i,j] = 1&lt;BR /&gt;then combs[i,j] = initial_combs[i,j];&lt;BR /&gt;else combs[i,count] = 1;&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;combs = initial_combs // combs;&lt;BR /&gt;print combs;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note; I have combined the initial_combs matrix with the new combs. If I had 3 factors I would stop here. As I had 4 factors I have to do another set of calculations:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;combss = j(8,m,0);&lt;BR /&gt;count = 4;&lt;BR /&gt;do i = 1 to 8;&lt;BR /&gt;do j = 1 to m;&lt;BR /&gt;if combs[i,j] = 1&lt;BR /&gt;then combss[i,j] = combs[i,j];&lt;BR /&gt;else combss[i,count] = 1;&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;combss = combs // combss;&lt;BR /&gt;print combss;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the last set, I have used the previous&amp;nbsp;combined matrix (initial_combs with the new combs) and added D to the existing combinations. Again I had to combine the previous combinations (combs) with the new one (combss) to get the full set of combinations for m=4.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I want to work out the combinations for m=5 I have to add another set of calculations.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basically this will make the program very long. I need to combine all these separate sets of calculations into one loop maybe. The loop should depend on the number of factor (3, 4, 5,.....) I have tried to figure out how to do it but I couldn't. It is greatly appreciated if anyone could help&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;DJ&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 14:06:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627484#M4978</guid>
      <dc:creator>DJ20</dc:creator>
      <dc:date>2020-02-26T14:06:21Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the Combinations of factor levels in SAS/IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627485#M4979</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/313544"&gt;@DJ20&lt;/a&gt;&amp;nbsp;Welcome to the SAS Community &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I moved your post to the SAS/IML forum.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 14:08:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627485#M4979</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-02-26T14:08:02Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the Combinations of factor levels in SAS/IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627487#M4980</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;I am trying to figure out the number of combinations of factors in factorial design&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The number of combinations, as your code states, is 2**m.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;After this, I am confused, as you have solved the problem. Could you please explain further?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 14:10:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627487#M4980</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-02-26T14:10:08Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the Combinations of factor levels in SAS/IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627490#M4981</link>
      <description>&lt;P&gt;I need to put them in a matrix form so I can use it in further calculations.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to combine my code so it depends on the number of factors. I.e if I have 3 factors it will calculate the number of combination swhich is 2**3 = 8 and output the matrix of 0's and 1's as well!!&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 14:16:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627490#M4981</guid>
      <dc:creator>DJ20</dc:creator>
      <dc:date>2020-02-26T14:16:57Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the Combinations of factor levels in SAS/IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627492#M4982</link>
      <description>&lt;P&gt;You mention a matrix of zeros and ones, but your initial example used combinations of letters. What do you want the matrix to look like? For n=2, do you want the character matrix&lt;/P&gt;
&lt;P&gt;"1"&lt;/P&gt;
&lt;P&gt;"A"&lt;/P&gt;
&lt;P&gt;"B"&lt;/P&gt;
&lt;P&gt;"A B"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;or do you want the numerical matrix&amp;nbsp;&lt;/P&gt;
&lt;P&gt;{0 0,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;0 1,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;1 0,&lt;/P&gt;
&lt;P&gt;1 1};&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 14:20:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627492#M4982</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2020-02-26T14:20:44Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the Combinations of factor levels in SAS/IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627494#M4983</link>
      <description>&lt;P&gt;I want the numerical matrix as it is easier for me to use it later on with the rest of the calculations in my program.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;btw the numerical matrix should be:&lt;/P&gt;&lt;P&gt;{0 0,&lt;/P&gt;&lt;P&gt;&amp;nbsp;1 0,&lt;/P&gt;&lt;P&gt;&amp;nbsp;0 1,&lt;/P&gt;&lt;P&gt;1 1};&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 14:25:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627494#M4983</guid>
      <dc:creator>DJ20</dc:creator>
      <dc:date>2020-02-26T14:25:08Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the Combinations of factor levels in SAS/IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627496#M4984</link>
      <description>&lt;P&gt;If the order of the rows of the matrix is not important then the problem is equivalent to counting in binary.&amp;nbsp;&amp;nbsp; For example&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;

start FullFactorial( n );
  r = 2 ## n;
  x = j(r, n);
  pot = 2 ## ( (n-1):0 );
  do i = 1 to r;
    x[i, ] = band( i-1, pot ) &amp;gt; 0;
  end;
  return( x );
finish;

a = FullFactorial( 4 );
print a [colname={A B C D}];
 
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Feb 2020 14:26:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627496#M4984</guid>
      <dc:creator>IanWakeling</dc:creator>
      <dc:date>2020-02-26T14:26:59Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the Combinations of factor levels in SAS/IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627499#M4985</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/313544"&gt;@DJ20&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I need to put them in a matrix form so I can use it in further calculations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to combine my code so it depends on the number of factors. I.e if I have 3 factors it will calculate the number of combination swhich is 2**3 = 8 and output the matrix of 0's and 1's as well!!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you want them in a matrix, you could create a SAS data set using PROC FACTEX of your factorial design, and then read that into IML. A factorial combination with letters A B C D is less useful than a matrix of 0s and 1s that would be produced by PROC FACTEX.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And even if you don't have PROC FACTEX in your SAS license, a DO loop in PROC IML creating a matrix of zeros and ones would be much easier to produce and would be much more usable than a matrix containing combinations of letters ABCD.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 14:33:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627499#M4985</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-02-26T14:33:53Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the Combinations of factor levels in SAS/IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627500#M4986</link>
      <description>&lt;P&gt;Probably the simplest way is to use the EXPANDGRID function. You might need to sort the rows if the order is not in the order you want:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
/* example for n=3 */
m = expandgrid(0:1, 0:1, 0:1);
print m;
/* if you want, you can sort: */
call sort(m, ncol(m):1); 
print m;

/* n=5 columns */
m = expandgrid(0:1, 0:1, 0:1, 0:1, 0:1);
call sort(m, ncol(m):1); 
print m;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 14:34:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627500#M4986</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2020-02-26T14:34:52Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the Combinations of factor levels in SAS/IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627501#M4987</link>
      <description>&lt;P&gt;I am really sorry I have been using SAS for a short period. Can you please explain the code a little bit.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you think it can be changed to appear in the opposite order ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;DJ&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 14:36:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627501#M4987</guid>
      <dc:creator>DJ20</dc:creator>
      <dc:date>2020-02-26T14:36:32Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the Combinations of factor levels in SAS/IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627503#M4988</link>
      <description>&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 14:37:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627503#M4988</guid>
      <dc:creator>DJ20</dc:creator>
      <dc:date>2020-02-26T14:37:56Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the Combinations of factor levels in SAS/IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627509#M4989</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;

start FullFactorial( n );
  r = 2 ## n;
  x = j(r, n);
  pot = 2 ## ( 0:(n-1) );  /* reversed powers of 2 in this line */
  do i = 1 to r;
    x[i, ] = band( i-1, pot ) &amp;gt; 0;
  end;
  return( x );
finish;

a = FullFactorial( 4 );
print a [colname={A B C D}];
 
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Feb 2020 14:51:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627509#M4989</guid>
      <dc:creator>IanWakeling</dc:creator>
      <dc:date>2020-02-26T14:51:10Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the Combinations of factor levels in SAS/IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627510#M4990</link>
      <description>&lt;P&gt;Is it possible to put this within a do loop?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In my case I have different data sets with different number of factors and I need the program to work for all of them.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The number of factors are calculated using a do loop, I want to put the "expandgrid" function within the loop so it prints the matrix of 0's and 1's depending on the number of factors. Is it possible?&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 14:51:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627510#M4990</guid>
      <dc:creator>DJ20</dc:creator>
      <dc:date>2020-02-26T14:51:19Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the Combinations of factor levels in SAS/IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627513#M4991</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/313544"&gt;@DJ20&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Is it possible to put this within a do loop?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In my case I have different data sets with different number of factors and I need the program to work for all of them.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The number of factors are calculated using a do loop, I want to put the "expandgrid" function within the loop so it prints the matrix of 0's and 1's depending on the number of factors. Is it possible?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes, you can put any IML code into a DO loop. I am wondering if you really want a DO loop, or do you want to pass a parameter (the number of factors, let's say for example you have 5 in a particular data set) to some algorithm that then creates the matrix for 5 factors. You wouldn't want a DO loop that creates a matrix for 3 factors and then 4 factors and then 5 factors and so on ... you just want the matrix for 5 factors.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please consider using PROC FACTEX to create the desired matrix. SAS has already done the work of programming it and debugging it and testing it, so you don't have to write your own code with DO loops and then debug it and test it.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 14:55:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627513#M4991</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-02-26T14:55:14Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the Combinations of factor levels in SAS/IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627525#M4992</link>
      <description>&lt;P&gt;Yes now its in the order I wanted. Sorry again I didn't understand the job of every step in this code because I am fairly new to SAS.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I appreciate your help,&lt;/P&gt;&lt;P&gt;DJ&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 15:00:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627525#M4992</guid>
      <dc:creator>DJ20</dc:creator>
      <dc:date>2020-02-26T15:00:52Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the Combinations of factor levels in SAS/IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627528#M4993</link>
      <description>&lt;P&gt;Unfortunately my task is to write it in SAS/IML. Otherwise I would have used the FACTEX procedure for sure.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 15:03:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627528#M4993</guid>
      <dc:creator>DJ20</dc:creator>
      <dc:date>2020-02-26T15:03:21Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the Combinations of factor levels in SAS/IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627531#M4994</link>
      <description>&lt;P&gt;Yes, it is possible to generate the matrix for n factors. You can create a function that builds up the function call as a string and then uses CALL EXECUTE to generate the matrix:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
start AllZeroOneComb(n);
   if n=1 then return ( {0,1} );
   cmd = "m = expandgrid(";
   do i = 1 to n-1;
      cmd = cmd + "0:1, ";
   end;
   cmd = cmd + "0:1);";
   *print cmd;
   CALL EXECUTE(cmd);
   return (m);
finish;

do n = 1 to 3;
   m = AllZeroOneComb(n);
   print m;
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could also put Ian's algorithm into a function. Or any of the algorithms in the article,&lt;A href="https://blogs.sas.com/content/iml/2011/01/05/creating-a-matrix-with-all-combinations-of-zeros-and-ones.html" target="_self"&gt; "Creating a matrix with all combinations of zeros and ones."&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also pre-compute the matrices and stick them in a list. Then you don't need to recompute them for every data set.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 15:06:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627531#M4994</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2020-02-26T15:06:04Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the Combinations of factor levels in SAS/IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627534#M4995</link>
      <description>&lt;P&gt;Since you are new to SAS. you might want to share what you are actually trying to accomplish (other than to create a binary matrix), we might have additional advice. For example, if you are trying to compute all possible linear regression models on a set of k regressors, there are better ways&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 15:09:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627534#M4995</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2020-02-26T15:09:05Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the Combinations of factor levels in SAS/IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627845#M4996</link>
      <description>&lt;P&gt;Rick, an interesting idea to pre-compute the matrices and use a list.&amp;nbsp;&amp;nbsp; I have come up with a recursive direct product algorithm that is well suited to this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;

n = 5;
L = ListCreate( n );
L$1 = {0, 1};
do i = 1 to n - 1;
  L$(i+1) = ( L$1 @ j(2##i, 1) ) || ( {1,1} @ L$i );
end;

do i = 1 to n;
  print (L$i);
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The code uses the list syntax that came in with SAS/IML 14.3.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 10:45:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627845#M4996</guid>
      <dc:creator>IanWakeling</dc:creator>
      <dc:date>2020-02-27T10:45:17Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the Combinations of factor levels in SAS/IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627856#M4997</link>
      <description>&lt;P&gt;Very clever, Ian. I like it. As I have said in the past, you are the master of the Kronecker product.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For those who (like) me, need to deconstruct Ian's magic, here is an alternative computation if the inner loop, which might be more enlightening. You can also print A and B at each iteration.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i = 1 to n - 1;
  A = {0,1} @ j(2##i, 1);
  B = {1,1} @ L$i;
  L$(i+1) = A || B;
end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And for those who have not used lists in SAS/IML, see the article &lt;A href="https://blogs.sas.com/content/iml/2018/01/22/iml-lists-syntax.html" target="_self"&gt;"Create lists by using a natural syntax in SAS/IML"&lt;/A&gt; or watch this &lt;A href="https://blogs.sas.com/content/iml/2018/06/08/video-new-syntax-lists-sas-iml.html" target="_self"&gt;video on the list syntax&lt;/A&gt;.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For a discussion and example of storing matrices in a list, see &lt;A href="https://blogs.sas.com/content/iml/2020/03/04/store-matrices-list.html" target="_self"&gt;"Store pre-computed matrices in a list".&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Mar 2020 13:07:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-can-I-find-the-Combinations-of-factor-levels-in-SAS-IML/m-p/627856#M4997</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2020-03-09T13:07:57Z</dc:date>
    </item>
  </channel>
</rss>

