<?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: For each 'key' multiply matrices and store the output corresponding to the key in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/For-each-key-multiply-matrices-and-store-the-output/m-p/422023#M3922</link>
    <description>&lt;P&gt;Thanks a lot&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;. Assuming the files would be big (500 different keys - 500 different rows), how do I get back the result for a particular set of key?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, if I run this piece of code it gives me two want matrices for two rows in the data, which is perfect. But if there were 500 rows and I wanted to extract the one with&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;2017 USA XYZ Corporate&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;, i.e. want[2], what could I have done?&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 18 Dec 2017 15:57:49 GMT</pubDate>
    <dc:creator>ss59</dc:creator>
    <dc:date>2017-12-18T15:57:49Z</dc:date>
    <item>
      <title>For each 'key' multiply matrices and store the output corresponding to the key</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/For-each-key-multiply-matrices-and-store-the-output/m-p/421938#M3918</link>
      <description>&lt;P&gt;Hi all, I have the following two data sets:&lt;/P&gt;&lt;P&gt;&amp;nbsp;Data Set 1 with probabilities&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt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t;&lt;TD&gt;0.2&lt;/TD&gt;&lt;TD&gt;0.3&lt;/TD&gt;&lt;TD&gt;0.5&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data Set 2 with initial volumes:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;key 1&lt;/TD&gt;&lt;TD&gt;key 2&lt;/TD&gt;&lt;TD&gt;key 3&lt;/TD&gt;&lt;TD&gt;key 4&lt;/TD&gt;&lt;TD&gt;key 5&lt;/TD&gt;&lt;TD&gt;vol_st1&lt;/TD&gt;&lt;TD&gt;vol_st2&lt;/TD&gt;&lt;TD&gt;vol_st3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;France&lt;/TD&gt;&lt;TD&gt;2017&lt;/TD&gt;&lt;TD&gt;Approach A&lt;/TD&gt;&lt;TD&gt;Retail&lt;/TD&gt;&lt;TD&gt;Rating 1&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;France&lt;/TD&gt;&lt;TD&gt;2018&lt;/TD&gt;&lt;TD&gt;Approach B&lt;/TD&gt;&lt;TD&gt;Corporate&lt;/TD&gt;&lt;TD&gt;Rating 2&lt;/TD&gt;&lt;TD&gt;23&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;9&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Both data has same keys; here key_set_1 = France 2017 Approach A Retail Rating 1. Dataset 1 has probabilities and dataset 2 has volumes.&lt;/P&gt;&lt;P&gt;The task is, I have to calculate ending volumes of each state.&lt;/P&gt;&lt;P&gt;i.e. 1. create a matrix for each row (with unique keys) in data set 1. So for row 1, it would be:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;state1&lt;/TD&gt;&lt;TD&gt;state2&lt;/TD&gt;&lt;TD&gt;state3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;state1&lt;/TD&gt;&lt;TD&gt;0.2&lt;/TD&gt;&lt;TD&gt;0.2&lt;/TD&gt;&lt;TD&gt;0.6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;state2&lt;/TD&gt;&lt;TD&gt;0.3&lt;/TD&gt;&lt;TD&gt;0.1&lt;/TD&gt;&lt;TD&gt;0.6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;state3&lt;/TD&gt;&lt;TD&gt;0.1&lt;/TD&gt;&lt;TD&gt;0.3&lt;/TD&gt;&lt;TD&gt;0.6&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. create a vector of initial volumes&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3. And then multiply the vector in data set 2 with those matrices from data set 1, i.e. multiply the initial volumes with probabilities to get the end volume for each set of unique keys. For this example (for the first row), it would be multiplication of&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT color="#000000"&gt;20&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#000000"&gt;10&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#000000"&gt;12&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;with&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#333333"&gt;0.2&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#333333"&gt;0.2&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#333333"&gt;0.6&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#333333"&gt;0.3&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#333333"&gt;0.1&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#333333"&gt;0.6&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#333333"&gt;0.1&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#333333"&gt;0.3&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#333333"&gt;0.6&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;=&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT color="#000000"&gt;8.2&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#000000"&gt;8.6&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#000000"&gt;25.2&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks to the helpful folks from this forum, I have been able to partially achieve the first step, i.e. create the matrix&amp;nbsp;for each row of&amp;nbsp;data set 1. But, I am not sure how can I store the keys in an array and and store the outputs also corresponding to the same keys.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in the end result&amp;nbsp;we should be able to identify&amp;nbsp;input 1 (3*3 matrix), input 2 (1*3 vector) and output (1*3 vector) with&amp;nbsp;key_set_1. same for key_set_2, Key_set_3 and so on.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 18 Dec 2017 11:07:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/For-each-key-multiply-matrices-and-store-the-output/m-p/421938#M3918</guid>
      <dc:creator>ss59</dc:creator>
      <dc:date>2017-12-18T11:07:35Z</dc:date>
    </item>
    <item>
      <title>Re: For each 'key' multiply matrices and store the output corresponding to the key</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/For-each-key-multiply-matrices-and-store-the-output/m-p/421983#M3919</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Year (country method Segment) ( : $12.)
      pr_st1_2 pr_st1_3 pr_st1_1 pr_st2_1 pr_st2_3 pr_st2_2 pr_st3_1 pr_st3_2 pr_st3_3;
datalines;
2017 USA ABC Retail    0.2 0.6 0.2 0.3 0.6 0.1 0.1 0.3 0.6
2017 USA XYZ Corporate 0.1 0.5 0.4 0.2 0.6 0.2 0.2 0.3 0.5
;
run;
data key;
input country $  Year method $ Segment : $12.	vol_st1	vol_st2	vol_st3	;
cards;
USA	2017 ABC Retail 	 	20	10	12
USA	2017 XYZ Corporate	 	23	5	9
;
run;
data have;
merge have key;
by 	Year country method Segment	;
run;
 
proc sql noprint;
select name	into : vname separated by ' '
 from dictionary.columns
  where libname='WORK' and memname='HAVE' and lowcase(name) eqt 'pr_st'
   order by input(compress(scan(name,-2,'_'),,'kd'),best.),input(compress(scan(name,-1,'_'),,'kd'),best.);
quit;

proc iml;
use have nobs nobs;
read all var {&amp;amp;vname} into x;
read all var {vol_st1 vol_st2 vol_st3} into y;
close;
do i=1 to nobs;
temp=shape(x[i,],sqrt(ncol(x)));
want=(temp#t(y[i,]))[+,];
print want;
end;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 Dec 2017 14:11:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/For-each-key-multiply-matrices-and-store-the-output/m-p/421983#M3919</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-12-18T14:11:32Z</dc:date>
    </item>
    <item>
      <title>Re: For each 'key' multiply matrices and store the output corresponding to the key</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/For-each-key-multiply-matrices-and-store-the-output/m-p/421988#M3921</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;want = y[i,]*temp;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;would be&amp;nbsp;more simple.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Dec 2017 14:26:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/For-each-key-multiply-matrices-and-store-the-output/m-p/421988#M3921</guid>
      <dc:creator>IanWakeling</dc:creator>
      <dc:date>2017-12-18T14:26:22Z</dc:date>
    </item>
    <item>
      <title>Re: For each 'key' multiply matrices and store the output corresponding to the key</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/For-each-key-multiply-matrices-and-store-the-output/m-p/422023#M3922</link>
      <description>&lt;P&gt;Thanks a lot&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;. Assuming the files would be big (500 different keys - 500 different rows), how do I get back the result for a particular set of key?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, if I run this piece of code it gives me two want matrices for two rows in the data, which is perfect. But if there were 500 rows and I wanted to extract the one with&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;2017 USA XYZ Corporate&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;, i.e. want[2], what could I have done?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Dec 2017 15:57:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/For-each-key-multiply-matrices-and-store-the-output/m-p/422023#M3922</guid>
      <dc:creator>ss59</dc:creator>
      <dc:date>2017-12-18T15:57:49Z</dc:date>
    </item>
    <item>
      <title>Re: For each 'key' multiply matrices and store the output corresponding to the key</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/For-each-key-multiply-matrices-and-store-the-output/m-p/422206#M3931</link>
      <description>&lt;P&gt;I&amp;nbsp;think it would be best to&amp;nbsp;create&amp;nbsp;a single&amp;nbsp;matrix large enough to hold all 500 results.&amp;nbsp; Then the&amp;nbsp;matrix is filled up one row at a time for each iteration of the loop.&amp;nbsp;&amp;nbsp; Afterwards&amp;nbsp;results can be extracted by row sub setting.&amp;nbsp; The syntax would look like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;n = sqrt(ncol(x));
want = j(nobs, n);

do i = 1 to nobs;
  temp = shape(x[i,], n);
  want[i,] = y[i,]*temp;
end;

w2 = want[2,];
print w2;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Dec 2017 07:16:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/For-each-key-multiply-matrices-and-store-the-output/m-p/422206#M3931</guid>
      <dc:creator>IanWakeling</dc:creator>
      <dc:date>2017-12-19T07:16:01Z</dc:date>
    </item>
  </channel>
</rss>

