<?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: Creating Sum Variables For All Combinations Of 9 Variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-Sum-Variables-For-All-Combinations-Of-9-Variables/m-p/398090#M278370</link>
    <description>&lt;P&gt;Thanks for the response.&amp;nbsp; One clarification .... I need to execute these summations across all 10,000 observations.&lt;/P&gt;&lt;P&gt;Does that change the code / process having more than one row?&lt;/P&gt;</description>
    <pubDate>Fri, 22 Sep 2017 13:27:44 GMT</pubDate>
    <dc:creator>whajjar71</dc:creator>
    <dc:date>2017-09-22T13:27:44Z</dc:date>
    <item>
      <title>Creating Sum Variables For All Combinations Of 9 Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-Sum-Variables-For-All-Combinations-Of-9-Variables/m-p/397972#M278368</link>
      <description>&lt;P&gt;I am new to the SAS community and appreciate any help you can provide.&lt;/P&gt;&lt;P&gt;I have 9 variables - H1 ... H9.&amp;nbsp; I need to create new variables with the sums of each possible combination of those original 9.&lt;/P&gt;&lt;P&gt;Does anyone have code to complete that task?&amp;nbsp; Thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 22 Sep 2017 01:25:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-Sum-Variables-For-All-Combinations-Of-9-Variables/m-p/397972#M278368</guid>
      <dc:creator>whajjar71</dc:creator>
      <dc:date>2017-09-22T01:25:44Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Sum Variables For All Combinations Of 9 Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-Sum-Variables-For-All-Combinations-Of-9-Variables/m-p/397987#M278369</link>
      <description>&lt;P&gt;I take it you want all 2-element sums, 3-elements sums, .... 9-element sum, right?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could do some nested loops in a data step, but I'd suggest using PROC SUMMARY to generate a data set with all the combinations (from 1-way "combination" to 9-way).&amp;nbsp; Then read that data set and calculate hsum in a single assignment:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  id=1;
  array h {9} (1,2,4,8,16,32,64,128,256);
  output;
  id=2;
  do i=1 to 9;  h{i}=2*h{i};end;
  output;
run;

proc summary data=have (keep=id h1-h9 ) completetypes noprint missing chartype ;
  by id;
  class h1-h9  ;
  output out=need / ways;
  ways 1 to 9;
run;

data want;
  set need (drop=_freq_);
  hsum=sum(of h1-h9);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dataset NEED will have, for each ID,&amp;nbsp;511 observations (=2**9 - 1) with each possible combination of H values.&amp;nbsp; It will also have variables _WAY_&amp;nbsp; (1 for 1-way combo, 2 for 2-way combo, etc), and _TYPE_.&amp;nbsp; _TYPE_ will be a 9-digit strings of 1's and 0's corresponding to which H vars are present or missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this particular example HSUM will have every integer value from 1 to 511 for ID=1.&amp;nbsp;&amp;nbsp; For ID=2, hsum will have every even value from 2 to 1022.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Sep 2017 05:18:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-Sum-Variables-For-All-Combinations-Of-9-Variables/m-p/397987#M278369</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-09-22T05:18:38Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Sum Variables For All Combinations Of 9 Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-Sum-Variables-For-All-Combinations-Of-9-Variables/m-p/398090#M278370</link>
      <description>&lt;P&gt;Thanks for the response.&amp;nbsp; One clarification .... I need to execute these summations across all 10,000 observations.&lt;/P&gt;&lt;P&gt;Does that change the code / process having more than one row?&lt;/P&gt;</description>
      <pubDate>Fri, 22 Sep 2017 13:27:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-Sum-Variables-For-All-Combinations-Of-9-Variables/m-p/398090#M278370</guid>
      <dc:creator>whajjar71</dc:creator>
      <dc:date>2017-09-22T13:27:44Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Sum Variables For All Combinations Of 9 Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-Sum-Variables-For-All-Combinations-Of-9-Variables/m-p/398122#M278371</link>
      <description>&lt;P&gt;If you take a close look at my example, you'll see that it treats 2 rows, not just one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the requirement of this approach is that you need some identifier variable(s) to uniquely identify each row.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Sep 2017 14:50:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-Sum-Variables-For-All-Combinations-Of-9-Variables/m-p/398122#M278371</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-09-22T14:50:24Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Sum Variables For All Combinations Of 9 Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-Sum-Variables-For-All-Combinations-Of-9-Variables/m-p/398284#M278372</link>
      <description>&lt;P&gt;That will lead to 2^9 obs for one obs. Are sure you want this ?&lt;/P&gt;</description>
      <pubDate>Sat, 23 Sep 2017 12:29:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-Sum-Variables-For-All-Combinations-Of-9-Variables/m-p/398284#M278372</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-09-23T12:29:32Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Sum Variables For All Combinations Of 9 Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-Sum-Variables-For-All-Combinations-Of-9-Variables/m-p/398290#M278373</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards dlm=',';
input h1-h9;
cards;
1,2,4,8,16,32,64,128,256
;
run;
data want;
 set have;
 array h{*} h1-h9;
 array x{*} x1-x9;
 k=-1;
 do i=1 to 2**dim(x);
  rc=graycode(k,of x{*});
  sum=0;
  do j=1 to dim(x);
    sum+x{j}*h{j};
  end;
  output;
 end;
 keep h1-h9 sum k;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 23 Sep 2017 13:00:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-Sum-Variables-For-All-Combinations-Of-9-Variables/m-p/398290#M278373</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-09-23T13:00:28Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Sum Variables For All Combinations Of 9 Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-Sum-Variables-For-All-Combinations-Of-9-Variables/m-p/398322#M278374</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I hadn't heard of graycode before, so I looked it up on wikipedia.&amp;nbsp; I like the idea of using graycode to step through the combinations, but it would be nicer to avoid looping through all 9 products (h{i}*x{I}) to generate a sum for each graycode iteration.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And it seems that ought to be possible.&amp;nbsp; SAS defines graycode as "generate all combinations of n items &lt;EM&gt;&lt;STRONG&gt;in minimal change order&lt;/STRONG&gt;&lt;/EM&gt;", and Wikipedia says&amp;nbsp;graycode's&amp;nbsp;intrinsic property is to change only one member of the combination at a time.&amp;nbsp; I.e. each step either adds 1 element to the prior combination, or subtracts one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That suggests to take full advantage of graycode one could iteratively update sum instead calculating it from scratch (by either adding or subtracting one H value).&amp;nbsp;&amp;nbsp;What I don't immediately see is how to best identify the added or removed element, but one could improve efficiency a lot.&amp;nbsp; Especially for large datasets and long arrays.&lt;/P&gt;</description>
      <pubDate>Sat, 23 Sep 2017 23:08:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-Sum-Variables-For-All-Combinations-Of-9-Variables/m-p/398322#M278374</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-09-23T23:08:47Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Sum Variables For All Combinations Of 9 Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-Sum-Variables-For-All-Combinations-Of-9-Variables/m-p/398507#M278375</link>
      <description>&lt;PRE&gt;
@mkeintz
I agreed with you . That would lead to use SAS/IML . I doubted OP could have product IML.&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Sep 2017 12:49:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-Sum-Variables-For-All-Combinations-Of-9-Variables/m-p/398507#M278375</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-09-25T12:49:15Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Sum Variables For All Combinations Of 9 Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-Sum-Variables-For-All-Combinations-Of-9-Variables/m-p/398647#M278376</link>
      <description>&lt;P&gt;How about for a single obseration with 4 variables instead of 9 you generate the desired output so we can see what you think you want in a more concrete example.&lt;/P&gt;
&lt;P&gt;I'm not sure that some of the comments about numbers of variables involved is sinking in and maybe doing this by hand for a smaller set will demonstrate the concerns raised.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Sep 2017 19:52:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-Sum-Variables-For-All-Combinations-Of-9-Variables/m-p/398647#M278376</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-09-25T19:52:55Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Sum Variables For All Combinations Of 9 Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-Sum-Variables-For-All-Combinations-Of-9-Variables/m-p/398665#M278377</link>
      <description>&lt;P&gt;Here's a relatively simple way to do the task in a data step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id h1-h9;
datalines;
1 256 128  64 32 15 8  4 2 1
2 512 256 128 64 32 16 8 4 2
3 1 2 4  8 16 32  64 128 256
4 2 4 8 16 32 64 128 256 512
run;

%let dim=9;
%let ncombo=%eval(2**&amp;amp;dim);
data want (keep=id i h:);
  if _n_=1 then do;
    %grcode_setup(size=&amp;amp;dim);
  end;
  set have;
  array h{&amp;amp;dim};
  hcount=0;
  hsum=0;

  do i=1 to &amp;amp;ncombo;
    hcount= hcount + _graycode_sign{i};
    hsum =  hsum + h{_graycode_element{i}}*_graycode_sign{i} ;
    output;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;The IF "_n_=1" block calls a macro that iterates via a graycode progression through all combinations&amp;nbsp;of &amp;amp;DIM items. But instead of revising an array of dummies (as in the sas&amp;nbsp;&amp;nbsp;graycode function), it uses the underlying graycode algorithm to build two other&amp;nbsp;&amp;nbsp;arrays, focused on the element to be added to or removed from the combination:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;(1) _graycode_element{i}, the element to add or removed at the i'th iteration&lt;/P&gt;
&lt;P&gt;&amp;nbsp;(2) _graycode_sign{i}, -1 (remove) or +1 (add) for the i'th iteration&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use these arrays later to identify elements to add/remove to maintain a running HSUM&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also if you actually do want to maintain an array of dummies, as in the graycode function, just add&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT face="courier new,courier"&gt;array dum{&amp;amp;dim} (&amp;amp;dim*0);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;prior to the do loop.&amp;nbsp;&amp;nbsp; And&amp;nbsp; inside the do loop add:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;FONT face="courier new,courier"&gt;dum{_graycode_element{i}} = dum{_graycode_element{i}}&amp;nbsp;+ _graycode_sign{i};&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's the macro being called:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro grcode_setup(size=);
  %local size nc;
  %let nc= %eval(2**&amp;amp;size);

  array _graycode_element{&amp;amp;nc} _temporary_ (&amp;amp;nc*1);
  array _graycode_sign{&amp;amp;nc}    _temporary_ (&amp;amp;nc*0);
  do _digit=1 to &amp;amp;size;
    _d=_digit;
    do _i = 2**(_digit-1)+1 to &amp;amp;nc by 2**_digit;
      _graycode_element{_i}=_digit; 
      _graycode_sign{_i}=sign(_d);
      _d=-1*_d;
    end;
  end;
  drop _digit _d _i;
%mend grcode_setup;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notice&amp;nbsp;the macro does not&amp;nbsp;populate the sequence from I=1 to &amp;amp;NC.&amp;nbsp;&amp;nbsp; First it identifies all the iterations in which the first element of the array changes.&amp;nbsp; For element 1, the sequence is 0110, meaning it changes every 2nd iteration, starting with iteration 2.&amp;nbsp; For element 2, the sequence is 00111100 (changing every 4th iteration, starting with iteration 3).&amp;nbsp; Etc. Etc.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Sep 2017 20:44:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-Sum-Variables-For-All-Combinations-Of-9-Variables/m-p/398665#M278377</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-09-25T20:44:48Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Sum Variables For All Combinations Of 9 Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-Sum-Variables-For-All-Combinations-Of-9-Variables/m-p/399078#M278378</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;P&gt;What I don't immediately see is how to best identify the added or removed element, but one could improve efficiency a lot.&amp;nbsp;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;RC marks the spot:&lt;/P&gt;&lt;PRE&gt;data have;
infile cards dlm=',';
input h1-h9;
cards;
1,2,4,8,16,32,64,128,256
;
run;
data want2;
  set have;
  array h{*} h1-h9;
  array x{*} x1-x9;
  k=-1;
  sum=0;
  rc=graycode(k,of x{*});
  output;
  do i=1 to 2**dim(x)-1;
    rc=graycode(k,of x{*});
    if x{rc} then
      sum=sum+h{rc};
    else
      sum=sum-h{rc};
    output;
    end;
  keep h1-h9 sum k;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Sep 2017 09:12:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-Sum-Variables-For-All-Combinations-Of-9-Variables/m-p/399078#M278378</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2017-09-27T09:12:39Z</dc:date>
    </item>
  </channel>
</rss>

