<?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: neat way to  sum all Combinations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/neat-way-to-sum-all-Combinations/m-p/278267#M55966</link>
    <description>&lt;P&gt;Thank you, RicK!&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 17 Jun 2016 18:24:32 GMT</pubDate>
    <dc:creator>Ying</dc:creator>
    <dc:date>2016-06-17T18:24:32Z</dc:date>
    <item>
      <title>neat way to  sum all Combinations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/neat-way-to-sum-all-Combinations/m-p/278232#M55958</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I have data with variable x1, x2, x3, and I need to find all the possible combination of 3 (there are 7 ). &amp;nbsp;Is there any elegant way to do this instead of list all the combination? &amp;nbsp;Because I might end up with 5 or 10 variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; input x1 x2 x3;&lt;/P&gt;
&lt;P&gt;datalines;&lt;/P&gt;
&lt;P&gt;1 2 3&lt;/P&gt;
&lt;P&gt;3 4 5&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data all;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; set test;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/***is there any neat way to do this combination, instead of hard coded all the combinations?***/&lt;/P&gt;
&lt;P&gt;&amp;nbsp;comb1 = x1;&lt;/P&gt;
&lt;P&gt;comb2 = x2;&lt;/P&gt;
&lt;P&gt;comb3 = x3;&lt;/P&gt;
&lt;P&gt;comb12 = sum(x1, x2);&lt;/P&gt;
&lt;P&gt;comb13 = sum(x1, x3);&lt;/P&gt;
&lt;P&gt;comb23 = sum(x2, x3);&lt;/P&gt;
&lt;P&gt;comb123 = sum(x1, x2, x3);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thank you!&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jun 2016 15:52:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/neat-way-to-sum-all-Combinations/m-p/278232#M55958</guid>
      <dc:creator>Ying</dc:creator>
      <dc:date>2016-06-17T15:52:17Z</dc:date>
    </item>
    <item>
      <title>Re: neat way to  sum all Combinations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/neat-way-to-sum-all-Combinations/m-p/278247#M55962</link>
      <description>&lt;P&gt;Have you looked at the &lt;A href="http://support.sas.com/documentation/cdl/en/lefunctionsref/67960/HTML/default/viewer.htm#n0rag3bzl2l7dsn16xc6bwufqg5a.htm" target="_self"&gt;ALLCOMB function&lt;/A&gt;? &amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS﻿&lt;/a&gt;&amp;nbsp;also has a nice series of articles about &lt;A href="http://blogs.sas.com/content/iml/2013/09/30/generate-combinations-in-sas.html" target="_self"&gt;generating combinations in SAS&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jun 2016 17:09:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/neat-way-to-sum-all-Combinations/m-p/278247#M55962</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2016-06-17T17:09:39Z</dc:date>
    </item>
    <item>
      <title>Re: neat way to  sum all Combinations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/neat-way-to-sum-all-Combinations/m-p/278254#M55964</link>
      <description>&lt;P&gt;Yes, you can do this, probably in several ways. The following generates all binary sequences between 1 and 2**3-1 = 7:&lt;/P&gt;
&lt;P&gt;001&lt;/P&gt;
&lt;P&gt;010&lt;/P&gt;
&lt;P&gt;011&lt;/P&gt;
&lt;P&gt;,,,&lt;/P&gt;
&lt;P&gt;111&lt;/P&gt;
&lt;P&gt;The sum that you want is coef[1]*x1 + coef[2]*x2 + coef[3]*x3, where coef[i] is the i_th element of the binary sequence (thought of as a number). You can use the&amp;nbsp;SUBSTR function is used to extract the i_th coefficient.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data all;
set test;
array x[3] x1-x3;
array comb[7] c1-c7;
label c1="comb3" c2="comb2" c3="comb23"
      c4="comb1" c5="comb13" c6="comb12" c7="comb123";
n = dim(x);
do i = 1 to 2**n - 1;         /* count 1 to 2**n - 1 */
   s = put(i, Binary3.);      /* generate binary representation */
   sum = 0;
   do k = 1 to n;             /* sum = SUM of s[k]*x[k] */
      coef = substr(s,k,1);
      sum + coef*x[k];
   end;
   comb[i] = sum;             /* store sum for this combination of variables */
end;   
output;
keep x1-x3 c1-c7;
run;
proc print label; 
var x1-x3 c4 c2 c1 c6 c5 c3 c7;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notice that my algorithm does not generate the values in the same order as you did. (You computed sums of variables taken one at a time, taken two at a time, and taken three at a time.) If necessary, I'm sure others on the list can modify my idea to give the results in a different order.&amp;nbsp; To help compare my approach with your, I assigned labels to each variable that agree with your variable names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The algorithm is explained further in the article&amp;nbsp;&lt;A href="http://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;</description>
      <pubDate>Fri, 17 Jun 2016 17:57:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/neat-way-to-sum-all-Combinations/m-p/278254#M55964</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2016-06-17T17:57:03Z</dc:date>
    </item>
    <item>
      <title>Re: neat way to  sum all Combinations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/neat-way-to-sum-all-Combinations/m-p/278267#M55966</link>
      <description>&lt;P&gt;Thank you, RicK!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jun 2016 18:24:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/neat-way-to-sum-all-Combinations/m-p/278267#M55966</guid>
      <dc:creator>Ying</dc:creator>
      <dc:date>2016-06-17T18:24:32Z</dc:date>
    </item>
    <item>
      <title>Re: neat way to  sum all Combinations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/neat-way-to-sum-all-Combinations/m-p/362616#M85681</link>
      <description>&lt;P&gt;I know this post was some time ago, but I found in useful in working out how to do a similar thing but with more variables.&amp;nbsp; The thing I'd like to be able to do but I can't think how is to know which of the combinations were 1, 2, 3 etc&amp;nbsp;factor combinations - any suggestions of where to start looking for an answer.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Jo&lt;/P&gt;</description>
      <pubDate>Tue, 30 May 2017 05:22:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/neat-way-to-sum-all-Combinations/m-p/362616#M85681</guid>
      <dc:creator>jo1</dc:creator>
      <dc:date>2017-05-30T05:22:14Z</dc:date>
    </item>
    <item>
      <title>Re: neat way to  sum all Combinations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/neat-way-to-sum-all-Combinations/m-p/362662#M85692</link>
      <description>&lt;P&gt;I assume you figured out how to adjust the dimensions of the arrays and the width of the BINARYw. format., The important change is to add S to the KEEP statement. The variable&amp;nbsp;S is a binary string that&amp;nbsp;indicates which variables are in the combination. Thus '10011' represents x1, x2, and x5.&lt;/P&gt;</description>
      <pubDate>Tue, 30 May 2017 10:21:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/neat-way-to-sum-all-Combinations/m-p/362662#M85692</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-05-30T10:21:49Z</dc:date>
    </item>
    <item>
      <title>Re: neat way to  sum all Combinations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/neat-way-to-sum-all-Combinations/m-p/362910#M85794</link>
      <description>Thanks yes, after a bit I worked out to increase the dimensions of the arrays and adjusted the width of the BINARY. I did try adding s to the KEEP statement but I'm assuming it would have been just the last combination that it was showing.&lt;BR /&gt;But continuing that line of thought I created a new table with the number of combinations and their BINARYw number (only the 1's)&lt;BR /&gt;data new;&lt;BR /&gt;do i = 1 to 29;&lt;BR /&gt;s = compress(put(i,Binary5.),"0");&lt;BR /&gt;output;&lt;BR /&gt;I needed to transpose the data anyway so the Combinations (columns) become a variable and then I was able to join the two tables together so instead of having Col1 - Col29 I returned the binary tag created in the new table 1, 11, 111 etc which identified them.&lt;BR /&gt;It is a bit of mucking around but I couldn't think of another way to do it. I'm not sure if I should have been able to label the column headings with the new table results somehow.&lt;BR /&gt;</description>
      <pubDate>Tue, 30 May 2017 22:40:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/neat-way-to-sum-all-Combinations/m-p/362910#M85794</guid>
      <dc:creator>jo1</dc:creator>
      <dc:date>2017-05-30T22:40:50Z</dc:date>
    </item>
    <item>
      <title>Re: neat way to  sum all Combinations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/neat-way-to-sum-all-Combinations/m-p/368569#M87918</link>
      <description>&lt;P&gt;I have been able to adjust this code to use when I have larger values of n but this gets out of hand quickly.&amp;nbsp; Can you give a clue on how to alter this code to only look at combinations of a set number of values.&amp;nbsp; Eg I might have 5 columns but I don't want to look at all possible 31 combinations but only the combinations that have 3 sample points in each combination = 10 combinations.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jun 2017 03:51:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/neat-way-to-sum-all-Combinations/m-p/368569#M87918</guid>
      <dc:creator>jo1</dc:creator>
      <dc:date>2017-06-20T03:51:47Z</dc:date>
    </item>
    <item>
      <title>Re: neat way to  sum all Combinations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/neat-way-to-sum-all-Combinations/m-p/368653#M87931</link>
      <description>&lt;P&gt;The easiest way is to use the ALLCOMB function, as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4"&gt;@ChrisHemedinger&lt;/a&gt;&amp;nbsp;suggested. See the references in his post. If you want to stay with the binary approach, restrict your attention to&amp;nbsp;strings that have exactly three 1s. &amp;nbsp;If you need further help, I suggest you open a new thread, explain your problem, and post the code that you are using.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jun 2017 10:00:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/neat-way-to-sum-all-Combinations/m-p/368653#M87931</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-06-20T10:00:13Z</dc:date>
    </item>
  </channel>
</rss>

