<?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: Calculate averages of every possible combination (only once) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-averages-of-every-possible-combination-only-once/m-p/360159#M84738</link>
    <description>&lt;P&gt;There are definitely ways to get ALLCOMBI to do this, but I have always programmed it by brute force. &amp;nbsp;This will get you the data, but getting the report is up to you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;array diffs {11} diff01-diff11;&lt;/P&gt;
&lt;P&gt;array inc {11} include01-include11;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;do until (done);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp;set have end=done;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp;diffs{subject}=diff;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;end;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;length subjlist $ 11;&lt;/P&gt;
&lt;P&gt;do include01=0, 1;&lt;/P&gt;
&lt;P&gt;do include02=0, 1;&lt;/P&gt;
&lt;P&gt;do include03=0, 1;&lt;/P&gt;
&lt;P&gt;do include04=0, 1;&lt;/P&gt;
&lt;P&gt;do include05=0, 1;&lt;/P&gt;
&lt;P&gt;do include06=0, 1;&lt;/P&gt;
&lt;P&gt;do include07=0, 1;&lt;/P&gt;
&lt;P&gt;do include08=0, 1;&lt;/P&gt;
&lt;P&gt;do include09=0, 1;&lt;/P&gt;
&lt;P&gt;do include10=0, 1;&lt;/P&gt;
&lt;P&gt;do include11=0, 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;total=0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;subjlist=' ';&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;do i=1 to 11;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;total + inc{i} * diffs{i};&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;substr(subjlist, i, 1) = put(inc{i}, 1.);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;mean = total / sum(of inc01-inc11);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;output;&lt;/P&gt;
&lt;P&gt;end; end; end; end; end; end; end; end; end; end; end;&lt;/P&gt;
&lt;P&gt;keep diff01-diff11 subjlist mean;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's untested code, so might need some tweaking. &amp;nbsp;But it should generate all the information you're seeking (plus an extra record ... the empty set with a mean of 0).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;With a correction above.&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Sat, 20 May 2017 09:44:03 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2017-05-20T09:44:03Z</dc:date>
    <item>
      <title>Calculate averages of every possible combination (only once)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-averages-of-every-possible-combination-only-once/m-p/360121#M84724</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I will like to create the averages of all possible combinations using the dataset which&amp;nbsp;can be created from the SAS code below please.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data input_data;
  subject = 1; diff = 4.0; output;
  subject = 2; diff = 10.4; output;
  subject = 3; diff = 11.8; output;
  subject = 4; diff = 11.9; output;
  subject = 5; diff = 17.4; output;
  subject = 6; diff = 20.4; output;
  subject = 7; diff = 21.1; output;
  subject = 8; diff = 25.0; output;
  subject = 9; diff = 25.6; output;
  subject = 10; diff = 28.8; output;
  subject = 11; diff = 29.8; output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I am trying&amp;nbsp;to get my output to be something similar to the image below please. I only want to calculate the averages between every combination once, like in the image below. I have tried doing this by using the cartesian join, however this creates every single average, and I only need 66 of them. I think I may have to use the allcombi function or Proc IML?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/8967iD0B2BA56D9F08F00/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="picture2.PNG" title="picture2.PNG" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks in advanced for your help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kriss&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2017 21:05:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-averages-of-every-possible-combination-only-once/m-p/360121#M84724</guid>
      <dc:creator>djrisks</dc:creator>
      <dc:date>2017-05-19T21:05:52Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate averages of every possible combination (only once)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-averages-of-every-possible-combination-only-once/m-p/360127#M84725</link>
      <description>&lt;P&gt;Here is one way:&lt;/P&gt;
&lt;PRE&gt;data input_data;
  subject = 1; diff = 4.0; output;
  subject = 2; diff = 10.4; output;
  subject = 3; diff = 11.8; output;
  subject = 4; diff = 11.9; output;
  subject = 5; diff = 17.4; output;
  subject = 6; diff = 20.4; output;
  subject = 7; diff = 21.1; output;
  subject = 8; diff = 25.0; output;
  subject = 9; diff = 25.6; output;
  subject = 10; diff = 28.8; output;
  subject = 11; diff = 29.8; output;
run;

proc sql;
  create table want as
   select a.subject,b.subject as subject2,
          mean(a.diff,b.diff) as average
     from input_data a, input_data b
       where a.subject le b.subject
         order by subject,subject2
  ;
quit;

proc transpose data=want 
               out=want (drop=_:) prefix=Subject;
  var average;
  by subject;
  id subject2;
run;

proc print data=want;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2017 21:37:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-averages-of-every-possible-combination-only-once/m-p/360127#M84725</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-05-19T21:37:18Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate averages of every possible combination (only once)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-averages-of-every-possible-combination-only-once/m-p/360159#M84738</link>
      <description>&lt;P&gt;There are definitely ways to get ALLCOMBI to do this, but I have always programmed it by brute force. &amp;nbsp;This will get you the data, but getting the report is up to you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;array diffs {11} diff01-diff11;&lt;/P&gt;
&lt;P&gt;array inc {11} include01-include11;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;do until (done);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp;set have end=done;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp;diffs{subject}=diff;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;end;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;length subjlist $ 11;&lt;/P&gt;
&lt;P&gt;do include01=0, 1;&lt;/P&gt;
&lt;P&gt;do include02=0, 1;&lt;/P&gt;
&lt;P&gt;do include03=0, 1;&lt;/P&gt;
&lt;P&gt;do include04=0, 1;&lt;/P&gt;
&lt;P&gt;do include05=0, 1;&lt;/P&gt;
&lt;P&gt;do include06=0, 1;&lt;/P&gt;
&lt;P&gt;do include07=0, 1;&lt;/P&gt;
&lt;P&gt;do include08=0, 1;&lt;/P&gt;
&lt;P&gt;do include09=0, 1;&lt;/P&gt;
&lt;P&gt;do include10=0, 1;&lt;/P&gt;
&lt;P&gt;do include11=0, 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;total=0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;subjlist=' ';&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;do i=1 to 11;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;total + inc{i} * diffs{i};&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;substr(subjlist, i, 1) = put(inc{i}, 1.);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;mean = total / sum(of inc01-inc11);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;output;&lt;/P&gt;
&lt;P&gt;end; end; end; end; end; end; end; end; end; end; end;&lt;/P&gt;
&lt;P&gt;keep diff01-diff11 subjlist mean;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's untested code, so might need some tweaking. &amp;nbsp;But it should generate all the information you're seeking (plus an extra record ... the empty set with a mean of 0).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;With a correction above.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 20 May 2017 09:44:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-averages-of-every-possible-combination-only-once/m-p/360159#M84738</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-05-20T09:44:03Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate averages of every possible combination (only once)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-averages-of-every-possible-combination-only-once/m-p/360179#M84747</link>
      <description>&lt;P&gt;Since&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt;&amp;nbsp;has already given you sql solution.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want present IML solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data input_data;
  subject = 1; diff = 4.0; output;
  subject = 2; diff = 10.4; output;
  subject = 3; diff = 11.8; output;
  subject = 4; diff = 11.9; output;
  subject = 5; diff = 17.4; output;
  subject = 6; diff = 20.4; output;
  subject = 7; diff = 21.1; output;
  subject = 8; diff = 25.0; output;
  subject = 9; diff = 25.6; output;
  subject = 10; diff = 28.8; output;
  subject = 11; diff = 29.8; output;
run;
options missing=' ';
proc iml;
use input_data nobs nobs;
read all var {diff};
close;
t=t(1:nobs);
w=expandgrid(diff,diff)[,:]||expandgrid(t,t);
want=full(w);
idx=loc(row(want)&amp;gt;col(want));
want[idx]=.;

char_diff=char(diff);
mattrib want r=char_diff c=char_diff l='';
print want;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/8969i349848E612F1A9E8/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="x.png" title="x.png" /&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 20 May 2017 05:52:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-averages-of-every-possible-combination-only-once/m-p/360179#M84747</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-05-20T05:52:09Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate averages of every possible combination (only once)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-averages-of-every-possible-combination-only-once/m-p/360186#M84749</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt;&amp;nbsp;, Brilliant!, If you don't mind and have the time, Could you please offer a line or two explanation to help me understand the proc sql solution picks all combinations. I so want to understand Please!&lt;/P&gt;</description>
      <pubDate>Sat, 20 May 2017 11:44:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-averages-of-every-possible-combination-only-once/m-p/360186#M84749</guid>
      <dc:creator>MarkWik</dc:creator>
      <dc:date>2017-05-20T11:44:08Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate averages of every possible combination (only once)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-averages-of-every-possible-combination-only-once/m-p/360187#M84750</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/34445"&gt;@MarkWik&lt;/a&gt;:&amp;nbsp;It's called a Cartesian product. The SAS note (&lt;A href="http://support.sas.com/kb/25/270.html" target="_blank"&gt;http://support.sas.com/kb/25/270.html&lt;/A&gt;) describes the result as "&lt;/P&gt;
&lt;P&gt;When joining multiple tables, the default behavior of PROC SQL is to build all possible combinations between the tables. This is also known as the Cartesian Product.&lt;/P&gt;
&lt;P&gt;The following note will be written to the SAS log when a Cartesian Product is created:&lt;/P&gt;
&lt;PRE&gt;  
&lt;STRONG&gt;NOTE:&lt;/STRONG&gt; The execution of this query involves performing one or
      more Cartesian product joins that can not be optimized. &lt;/PRE&gt;
&lt;P&gt;"&lt;/P&gt;
&lt;P&gt;As such, the code I suggested does such a join (including computing the averages) then, according to &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10604"&gt;@djrisks&lt;/a&gt;'s&amp;nbsp;specs, eliminated the lower half of the diagonal, and ordered the rows and columns by subject number.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Sat, 20 May 2017 12:21:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-averages-of-every-possible-combination-only-once/m-p/360187#M84750</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-05-20T12:21:56Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate averages of every possible combination (only once)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-averages-of-every-possible-combination-only-once/m-p/360188#M84751</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt;! This is so helpful!&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;I like the IML solution too, and thank you for helping me with it!&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;thank you for the array&amp;nbsp;method!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 20 May 2017 12:51:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-averages-of-every-possible-combination-only-once/m-p/360188#M84751</guid>
      <dc:creator>djrisks</dc:creator>
      <dc:date>2017-05-20T12:51:49Z</dc:date>
    </item>
  </channel>
</rss>

