<?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: Find a Sum of Product with elements from a Data Column or a Vector in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Find-a-Sum-of-Product-with-elements-from-a-Data-Column-or-a/m-p/771316#M5671</link>
    <description>&lt;P&gt;A non-IML suggestion is found &lt;A href="https://communities.sas.com/t5/SAS-Programming/How-to-summarize-a-column-with-combination-of-sum-and-product/m-p/771315/highlight/true#M244732" target="_blank" rel="noopener"&gt;here&lt;/A&gt;.&lt;/P&gt;</description>
    <pubDate>Thu, 30 Sep 2021 07:24:18 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2021-09-30T07:24:18Z</dc:date>
    <item>
      <title>Find a Sum of Product with elements from a Data Column or a Vector</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Find-a-Sum-of-Product-with-elements-from-a-Data-Column-or-a/m-p/771275#M5669</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset with two columns like the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data temp;&lt;/P&gt;
&lt;P&gt;input item count;&lt;/P&gt;
&lt;P&gt;datalines;&lt;/P&gt;
&lt;P&gt;a1&amp;nbsp; &amp;nbsp;n1&lt;/P&gt;
&lt;P&gt;a2&amp;nbsp; &amp;nbsp;n2&lt;/P&gt;
&lt;P&gt;a3&amp;nbsp; &amp;nbsp;n3&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;ak&amp;nbsp; &amp;nbsp;nk&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;Basically, {n1, n2, ...,n k} are the counts of {a1,... ak}. I would like to compute the number b= (n1*n2 + n1*n3+ ... +n1*nk) + (n2*n3 + n2*n4+...n2*nk) + ...+&lt;/P&gt;
&lt;P&gt;(n_(k-1) *nk). Is there any efficient way to do it?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Sep 2021 02:18:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Find-a-Sum-of-Product-with-elements-from-a-Data-Column-or-a/m-p/771275#M5669</guid>
      <dc:creator>Macro</dc:creator>
      <dc:date>2021-09-30T02:18:19Z</dc:date>
    </item>
    <item>
      <title>Re: Find a Sum of Product with elements from a Data Column or a Vector</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Find-a-Sum-of-Product-with-elements-from-a-Data-Column-or-a/m-p/771314#M5670</link>
      <description>&lt;P&gt;And you want to do this in IML ?&lt;/P&gt;</description>
      <pubDate>Thu, 30 Sep 2021 06:44:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Find-a-Sum-of-Product-with-elements-from-a-Data-Column-or-a/m-p/771314#M5670</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-09-30T06:44:03Z</dc:date>
    </item>
    <item>
      <title>Re: Find a Sum of Product with elements from a Data Column or a Vector</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Find-a-Sum-of-Product-with-elements-from-a-Data-Column-or-a/m-p/771316#M5671</link>
      <description>&lt;P&gt;A non-IML suggestion is found &lt;A href="https://communities.sas.com/t5/SAS-Programming/How-to-summarize-a-column-with-combination-of-sum-and-product/m-p/771315/highlight/true#M244732" target="_blank" rel="noopener"&gt;here&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Thu, 30 Sep 2021 07:24:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Find-a-Sum-of-Product-with-elements-from-a-Data-Column-or-a/m-p/771316#M5671</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-09-30T07:24:18Z</dc:date>
    </item>
    <item>
      <title>Re: Find a Sum of Product with elements from a Data Column or a Vector</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Find-a-Sum-of-Product-with-elements-from-a-Data-Column-or-a/m-p/771337#M5672</link>
      <description>&lt;P&gt;The straightforward way is efficient and simple to understand:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
input item $ count;
datalines;
a1   2
a2   4
a3   3
a5   2
a5   1
a6   5
;

proc iml;
use temp;
read all var "count" into n;
close;
k = nrow(n);

/* simplest way */
sum = 0;
do i = 1 to k-1;
   sum = sum + n[i]*sum(n[(i+1):k]);
end;
print sum;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If by "efficient" you mean "vectorized," you can get rid of the DO loop by noting that the SUM() function inside the loop is computing the cumulative sums of the vector n in reverse order.&amp;nbsp; Thus you could use the CUSUM function to compute the cumulative sums and then perform an inner product with n to get the sum:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* use cumulative sums of the reverse of n */
m = n[ k:1 ];   /* reverse n */
cs = cusum(m);  /* cumulative sums */
c = cs[k:1];    /* reverse the cumulative sums */
*print c;       /* if you need to see the cumulative sums */
/* inner product of n and (reverse) cumulative sums */
sum = n[1:(k-1)]` * c[2:k];   
print sum;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 Sep 2021 10:29:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Find-a-Sum-of-Product-with-elements-from-a-Data-Column-or-a/m-p/771337#M5672</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2021-09-30T10:29:55Z</dc:date>
    </item>
    <item>
      <title>Re: Find a Sum of Product with elements from a Data Column or a Vector</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Find-a-Sum-of-Product-with-elements-from-a-Data-Column-or-a/m-p/771381#M5673</link>
      <description>&lt;P&gt;Hi Rick,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Very nice solution. Actually my goal is to use this sum inside of my code to do some additional computation, instead of just printing it. How do I send this sum to a macro variable to save it and do further computation from inside proc iml and then close proc iml to proceed outside of proc iml? Can I use %let statement inside proc iml or it has other interface to send this sum out to a macro variable? Thanks.&lt;/P&gt;</description>
      <pubDate>Thu, 30 Sep 2021 14:12:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Find-a-Sum-of-Product-with-elements-from-a-Data-Column-or-a/m-p/771381#M5673</guid>
      <dc:creator>Macro</dc:creator>
      <dc:date>2021-09-30T14:12:22Z</dc:date>
    </item>
    <item>
      <title>Re: Find a Sum of Product with elements from a Data Column or a Vector</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Find-a-Sum-of-Product-with-elements-from-a-Data-Column-or-a/m-p/771489#M5674</link>
      <description>&lt;P&gt;&lt;SPAN&gt;CALL SYMPUT("MyMacro", char(sum));&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Sep 2021 20:25:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Find-a-Sum-of-Product-with-elements-from-a-Data-Column-or-a/m-p/771489#M5674</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2021-09-30T20:25:04Z</dc:date>
    </item>
    <item>
      <title>Re: Find a Sum of Product with elements from a Data Column or a Vector</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Find-a-Sum-of-Product-with-elements-from-a-Data-Column-or-a/m-p/773296#M5686</link>
      <description>&lt;P&gt;It appears this non-IML way is incorrect.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Oct 2021 00:00:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Find-a-Sum-of-Product-with-elements-from-a-Data-Column-or-a/m-p/773296#M5686</guid>
      <dc:creator>Macro</dc:creator>
      <dc:date>2021-10-11T00:00:50Z</dc:date>
    </item>
  </channel>
</rss>

