<?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: Getting all the combinations of a population of n observations into 2 clusters in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Getting-all-the-combinations-of-a-population-of-n-observations/m-p/671457#M201644</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/334058"&gt;@JohnX&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's call the groups 0 and 1. Then I would assign the first observation always to group 0 and the other &lt;EM&gt;n&lt;/EM&gt;-1 observations to the groups given by the binary digits of the integers 0, 1, ..., 2**(&lt;EM&gt;n&lt;/EM&gt;-1)-1. This covers all possible combinations up to switching the two groups. Here's a DATA step which creates variables g2, g3, ..., g&lt;EM&gt;n&lt;/EM&gt; containing the group numbers (i.e., 0 or 1) for the 2nd, 3rd, ..., &lt;EM&gt;n&lt;/EM&gt;-th observation:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let n=10;

data want(drop=i j);
array g[2:&amp;amp;n] g2-g&amp;amp;n;
do i=0 to 2**(&amp;amp;n-1)-1;
  do j=2 to &amp;amp;n;
    g[j]=~~band(i,2**(&amp;amp;n-j));
  end;
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Edit: I had tested three variants of this DATA step (some using the BINARY&lt;EM&gt;w&lt;/EM&gt;. format, some using $1-character arrays). The above version was the fastest for &lt;EM&gt;n&lt;/EM&gt;=25: it took about 20 seconds to create the 2**24=16,777,216 observations with 24 variables. You may want to do whatever needs to be done in the outer DO loop rather than actually OUTPUT that "matrix" -- whose information value is obviously very limited.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit 2: If run time is an issue, here's another version which is more than 10 times faster than the above suggestion: &amp;lt;2 seconds for &lt;EM&gt;n&lt;/EM&gt;=25.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro bitcomb(n);
%local i;
data want;
array g[2:&amp;amp;n] 3 g2-g&amp;amp;n;
%do i=2 %to &amp;amp;n;
  do g&amp;amp;i=0, 1;
%end;
    output;
%do i=2 %to &amp;amp;n;
  end;
%end;
run;
%mend bitcomb;

%bitcomb(10)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 22 Jul 2020 17:22:56 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2020-07-22T17:22:56Z</dc:date>
    <item>
      <title>Getting all the combinations of a population of n observations into 2 clusters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-all-the-combinations-of-a-population-of-n-observations/m-p/671423#M201627</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to have all the possible combinations to group n observations into two groups. For example, if n=3, and using the base 2, I get the set by writing 0 to 3 (as writing 4 to 7 is just the complement)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I could not find anything that would already exist, but I might be wrong. If there was something like this right away that would be great.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Because this does not exist, I thought the easiest way would be to generate a matrix with n columns and (1-2^(n+1))/(1-2) rows (in fact as mentioned previously only the first half is useful) and populate if with 0s and 1s to get something similar to this&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cn&amp;nbsp; &amp;nbsp;Cn-1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; C2&amp;nbsp; &amp;nbsp;C1&amp;nbsp; &amp;nbsp;C0&lt;/P&gt;&lt;P&gt;0&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;0&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;0&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;0&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;0&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I just do not know how to create such a matrix using SAS. That would be very easy if I could use indices, and the remaining of the euclidian division, but SAS do not know such things.&lt;/P&gt;&lt;P&gt;I was also thinking of asking SAS to write the row number-1 in base 2 with 1 column &amp;lt;=&amp;gt; 1 bit, but again, not sure it can do this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the only thing I need, as then that would be very easy to join on my table and using 0s and 1s to know to which final cluster my observation belongs.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please, no need to mention PROC IML, I do not have it on the system I use.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jul 2020 15:08:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-all-the-combinations-of-a-population-of-n-observations/m-p/671423#M201627</guid>
      <dc:creator>JohnX</dc:creator>
      <dc:date>2020-07-22T15:08:41Z</dc:date>
    </item>
    <item>
      <title>Re: Getting all the combinations of a population of n observations into 2 clusters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-all-the-combinations-of-a-population-of-n-observations/m-p/671434#M201631</link>
      <description>&lt;P&gt;Lets start with some basics: All combinations of what? A single variable? A list of variables? An arbitrary matrix?&lt;/P&gt;
&lt;P&gt;A concrete data step to show where you are starting might help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In a data step there are several functions related to doing permutations and combinations .&lt;/P&gt;
&lt;P&gt;You may want to read the documentation for functions Call Allcomb, Allcomb, Allcombi, Lexcomb and Lexcombi&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jul 2020 15:24:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-all-the-combinations-of-a-population-of-n-observations/m-p/671434#M201631</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-07-22T15:24:26Z</dc:date>
    </item>
    <item>
      <title>Re: Getting all the combinations of a population of n observations into 2 clusters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-all-the-combinations-of-a-population-of-n-observations/m-p/671457#M201644</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/334058"&gt;@JohnX&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's call the groups 0 and 1. Then I would assign the first observation always to group 0 and the other &lt;EM&gt;n&lt;/EM&gt;-1 observations to the groups given by the binary digits of the integers 0, 1, ..., 2**(&lt;EM&gt;n&lt;/EM&gt;-1)-1. This covers all possible combinations up to switching the two groups. Here's a DATA step which creates variables g2, g3, ..., g&lt;EM&gt;n&lt;/EM&gt; containing the group numbers (i.e., 0 or 1) for the 2nd, 3rd, ..., &lt;EM&gt;n&lt;/EM&gt;-th observation:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let n=10;

data want(drop=i j);
array g[2:&amp;amp;n] g2-g&amp;amp;n;
do i=0 to 2**(&amp;amp;n-1)-1;
  do j=2 to &amp;amp;n;
    g[j]=~~band(i,2**(&amp;amp;n-j));
  end;
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Edit: I had tested three variants of this DATA step (some using the BINARY&lt;EM&gt;w&lt;/EM&gt;. format, some using $1-character arrays). The above version was the fastest for &lt;EM&gt;n&lt;/EM&gt;=25: it took about 20 seconds to create the 2**24=16,777,216 observations with 24 variables. You may want to do whatever needs to be done in the outer DO loop rather than actually OUTPUT that "matrix" -- whose information value is obviously very limited.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit 2: If run time is an issue, here's another version which is more than 10 times faster than the above suggestion: &amp;lt;2 seconds for &lt;EM&gt;n&lt;/EM&gt;=25.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro bitcomb(n);
%local i;
data want;
array g[2:&amp;amp;n] 3 g2-g&amp;amp;n;
%do i=2 %to &amp;amp;n;
  do g&amp;amp;i=0, 1;
%end;
    output;
%do i=2 %to &amp;amp;n;
  end;
%end;
run;
%mend bitcomb;

%bitcomb(10)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jul 2020 17:22:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-all-the-combinations-of-a-population-of-n-observations/m-p/671457#M201644</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-07-22T17:22:56Z</dc:date>
    </item>
  </channel>
</rss>

