<?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: How to compute matrix product by group in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-compute-matrix-product-by-group/m-p/545473#M4610</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data datos;
input group $ x1 x2 w1 w2 w3;
datalines;
A	55.3	41.1	2	3	1
A	50.5	40	3	2	2
A	52.4	39.8	2	1	3
A	58.1	39	1	2	1
A	54	39.5	2	3	2
A	55.6	40.5	3	2	3
B	49.5	50.5	5	5	5
B	48.8	50	4	6	6
B	50.1	49.5	6	4	5
B	49.9	48	5	5	5
C	60.1	60	8	9	6
C	59.9	61	7	8	7
C	58	59	8	7	8
C	58.5	60.5	7	8	8
C	61.2	59.5	8	7	9
C	60	60	9	8	9
;
run;
%LET x = x1 x2;
%LET w = w1 w2 w3;

proc iml;
use datos;
read all var {group};
read all var { &amp;amp;x} into x[rowname=group colname = nr];
read all var { &amp;amp;w} into w[rowname=group colname = nc];
close datos;
levels=unique(group);
do i=1 to ncol(levels);
 idx=loc(group=levels[i]);
 x1=x[idx,];
 w1=w[idx,];
 a=a//t(x1)*w1;
 r=r//t(nr);
 g=g//repeat(levels[i],ncol(nr),1);
end;

create g var {g};
append;
close;
create w from a[r=r c=nc];
append from a[r=r];
close;
quit;
data want;
 merge g w;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 23 Mar 2019 11:59:07 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2019-03-23T11:59:07Z</dc:date>
    <item>
      <title>How to compute matrix product by group</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-compute-matrix-product-by-group/m-p/545415#M4608</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;I have a problem which I need to multiply &lt;FONT face="symbol"&gt;X1&lt;/FONT&gt; by 100 weights &lt;FONT face="symbol"&gt;W1 ... W100 &lt;/FONT&gt;and then sum them (weighted sum). My dataset has 100,000 cases&lt;/P&gt;&lt;P&gt;So I decided to compute matrix products in order to avoid loops.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data datos;
input group $ x1 x2 w1 w2 w3;
datalines;
A	55.3	41.1	2	3	1
A	50.5	40	3	2	2
A	52.4	39.8	2	1	3
A	58.1	39	1	2	1
A	54	39.5	2	3	2
A	55.6	40.5	3	2	3
B	49.5	50.5	5	5	5
B	48.8	50	4	6	6
B	50.1	49.5	6	4	5
B	49.9	48	5	5	5
C	60.1	60	8	9	6
C	59.9	61	7	8	7
C	58	59	8	7	8
C	58.5	60.5	7	8	8
C	61.2	59.5	8	7	9
C	60	60	9	8	9
;
run;
%LET x = x1 x2;
%LET w = w1 w2 w3;

proc iml;
use datos;
read all var { &amp;amp;x} into x[rowname=group colname = nr];
read all var { &amp;amp;w} into w[rowname=group colname = nc];
close datos;
mattrib a rowname = nr colname = nc label = "Overall sum";
a = t(x)*w;
print a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I currently obtain this:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Overall sum&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;w1&lt;/TD&gt;&lt;TD&gt;w2&lt;/TD&gt;&lt;TD&gt;w3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;x1&lt;/TD&gt;&lt;TD&gt;4495.8&lt;/TD&gt;&lt;TD&gt;4501.4&lt;/TD&gt;&lt;TD&gt;4489.4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;x2&lt;/TD&gt;&lt;TD&gt;4329.3&lt;/TD&gt;&lt;TD&gt;4332.6&lt;/TD&gt;&lt;TD&gt;4338.5&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;but now I need to calculate matrix product &lt;STRONG&gt;by group&lt;/STRONG&gt; in order to get this result:&lt;/P&gt;&lt;TABLE cellspacing="0" cellpadding="0" border="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;sums by group&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;w1&lt;/TD&gt;&lt;TD&gt;w2&lt;/TD&gt;&lt;TD&gt;w3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;x1&lt;/TD&gt;&lt;TD&gt;699.8&lt;/TD&gt;&lt;TD&gt;708.7&lt;/TD&gt;&lt;TD&gt;646.4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;x2&lt;/TD&gt;&lt;TD&gt;521.3&lt;/TD&gt;&lt;TD&gt;520.6&lt;/TD&gt;&lt;TD&gt;480.0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;x1&lt;/TD&gt;&lt;TD&gt;992.8&lt;/TD&gt;&lt;TD&gt;990.2&lt;/TD&gt;&lt;TD&gt;1040.3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;x2&lt;/TD&gt;&lt;TD&gt;989.5&lt;/TD&gt;&lt;TD&gt;990.5&lt;/TD&gt;&lt;TD&gt;1040.0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;x1&lt;/TD&gt;&lt;TD&gt;2803.2&lt;/TD&gt;&lt;TD&gt;2802.5&lt;/TD&gt;&lt;TD&gt;2802.7&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;x2&lt;/TD&gt;&lt;TD&gt;2818.5&lt;/TD&gt;&lt;TD&gt;2821.5&lt;/TD&gt;&lt;TD&gt;2818.5&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Sat, 23 Mar 2019 03:06:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-compute-matrix-product-by-group/m-p/545415#M4608</guid>
      <dc:creator>FcoAVC</dc:creator>
      <dc:date>2019-03-23T03:06:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to compute matrix product by group</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-compute-matrix-product-by-group/m-p/545454#M4609</link>
      <description>&lt;P&gt;Here is a way to implement the logic&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data datos;
input group $ x1 x2 w1 w2 w3;
datalines;
A	55.3	41.1	2	3	1
A	50.5	40	3	2	2
A	52.4	39.8	2	1	3
A	58.1	39	1	2	1
A	54	39.5	2	3	2
A	55.6	40.5	3	2	3
B	49.5	50.5	5	5	5
B	48.8	50	4	6	6
B	50.1	49.5	6	4	5
B	49.9	48	5	5	5
C	60.1	60	8	9	6
C	59.9	61	7	8	7
C	58	59	8	7	8
C	58.5	60.5	7	8	8
C	61.2	59.5	8	7	9
C	60	60	9	8	9
;
run;

%LET x = x1 x2;
%LET w = w1 w2 w3;

proc iml;

use datos;
   read all var {&amp;amp;x} into x;
   read all var {&amp;amp;w} into w;
   read all var {group} into group;
close datos;

u=unique(group);
x=t(x);
s = j(nrow(x), ncol(u));

do k=1 to 3;
   idx=loc(group=u[k]);
   do i=1 to nrow(x);
      do j=1 to ncol(u);
         s[i,j]=x[i, idx] * w[idx, j];
      end;
   end;
a=a//s;
end;

print a;

quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 23 Mar 2019 07:49:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-compute-matrix-product-by-group/m-p/545454#M4609</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-03-23T07:49:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to compute matrix product by group</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-compute-matrix-product-by-group/m-p/545473#M4610</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data datos;
input group $ x1 x2 w1 w2 w3;
datalines;
A	55.3	41.1	2	3	1
A	50.5	40	3	2	2
A	52.4	39.8	2	1	3
A	58.1	39	1	2	1
A	54	39.5	2	3	2
A	55.6	40.5	3	2	3
B	49.5	50.5	5	5	5
B	48.8	50	4	6	6
B	50.1	49.5	6	4	5
B	49.9	48	5	5	5
C	60.1	60	8	9	6
C	59.9	61	7	8	7
C	58	59	8	7	8
C	58.5	60.5	7	8	8
C	61.2	59.5	8	7	9
C	60	60	9	8	9
;
run;
%LET x = x1 x2;
%LET w = w1 w2 w3;

proc iml;
use datos;
read all var {group};
read all var { &amp;amp;x} into x[rowname=group colname = nr];
read all var { &amp;amp;w} into w[rowname=group colname = nc];
close datos;
levels=unique(group);
do i=1 to ncol(levels);
 idx=loc(group=levels[i]);
 x1=x[idx,];
 w1=w[idx,];
 a=a//t(x1)*w1;
 r=r//t(nr);
 g=g//repeat(levels[i],ncol(nr),1);
end;

create g var {g};
append;
close;
create w from a[r=r c=nc];
append from a[r=r];
close;
quit;
data want;
 merge g w;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 23 Mar 2019 11:59:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-compute-matrix-product-by-group/m-p/545473#M4610</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-03-23T11:59:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to compute matrix product by group</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-compute-matrix-product-by-group/m-p/545474#M4611</link>
      <description>&lt;P&gt;If the groups are unsorted, you can use &lt;A href="https://blogs.sas.com/content/iml/2011/11/01/the-unique-loc-trick-a-real-treat.html" target="_self"&gt;the UNIQUE-LOC technique&lt;/A&gt;&amp;nbsp;to obtain the rows for each group.&lt;/P&gt;
&lt;P&gt;What you do next&amp;nbsp;depends what you want to do with the&amp;nbsp;matrix products. Here is a loop that just prints out the products. You could also write them to a data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;u = unique(group);
do i = 1 to ncol(u);
   gIdx = loc(group = u[i]);  /* row numbers for i_th group */
   Xg = X[gIdx,];              /* subset data by group */
   Wg = W[gIdx,];
   A = Xg` * Wg;
   labl = "Group = " + u[i];
   print A[r=nr c=nc label = labl];
end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To write to a data set. Use the CREATE FROM statement to open the&amp;nbsp;data set BEFORE the loop. Use the APPEND FROM statement inside the loop, and close the data set outside the loop. &lt;A href="https://blogs.sas.com/content/iml/2015/03/09/writing-data-in-chunks.html" target="_self"&gt;Here is an example of writing data in a loop.&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For a complete example of a "BY-group " analysis in SAS/IML, see the article &lt;A href="https://blogs.sas.com/content/iml/2019/04/01/matrix-operations-by-groups.html" target="_self"&gt;"Matrix operations and BY groups"&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Apr 2019 14:33:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-compute-matrix-product-by-group/m-p/545474#M4611</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2019-04-01T14:33:31Z</dc:date>
    </item>
  </channel>
</rss>

