<?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: Writing to a data table from a matrix within a do loop  - iml in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Writing-to-a-data-table-from-a-matrix-within-a-do-loop-iml/m-p/424559#M3964</link>
    <description>&lt;P&gt;How about :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/********create parametrisation table*********/

data param_table;
infile datalines dsd;
length FieldName $20 FieldSourceTable $20;
input Matrix_Id Row_Order Column_Order IsZero IsRowResidual IsColumnResidual FieldName FieldSourceTable;
datalines;
1, 1, 1, 0, 1, 0, ., .
1, 1, 2, 0, 0, 0, xyz, table1
1, 1, 3, 0, 0, 0, abc, table1
1, 2, 1, 1, 0, 0, ., .
1, 2, 2, 0, 0, 0, pqr, table1
1, 2, 3, 0, 0, 0, mno, table1
1, 3, 1, 0, 0, 0, ab, table1
1, 3, 2, 0, 0, 0, pq, table1
1, 3, 3, 0, 1, 0, ., .
2, 1, 1, 1, 0, 0, ., .
2, 1, 2, 0, 0, 0, result1, table2
2, 1, 3, 0, 0, 0, result2, table2
2, 2, 1, 1, 0, 0, ., .
2, 2, 2, 0, 0, 0, sum1, table2
2, 2, 3, 0, 0, 0, sum2, table2
2, 3, 1, 0, 0, 0, prod1, table2
2, 3, 2, 0, 0, 0, prod2, table2
2, 3, 3, 1, 0, 0, ., .
;

proc sort data= param_table out= param_table;
by Matrix_Id Row_Order Column_Order;
quit;

proc sql;
create table param_tr as
select * from work.param_table
where Matrix_Id = 1;
quit;

proc sql;
create table param_flow as
select * from work.param_table
where Matrix_Id = 2;
quit;


/********create data table*********/

data table1;
input Year (country method Segment) ( : $12.)
 ABC XYZ PQR MNO AB PQ;
datalines;
2017 France ABC Retail 0.2 0.5 0.4 0.3 0.6 0.1
2017 France XYZ Corporate 0.1 0.5 0.4 0.2 0.6 0.2
;
run;

/*********build matrices and write out*********/

proc iml;
use param_tr; 
read all var {Column_Order Row_Order FieldName};
close;

idx = loc(FieldName ^= " ");
refNames = FieldName[idx];

use param_flow; 
read all var {Column_Order Row_Order FieldName};
close;

idx_2 = loc(FieldName ^= " ");
refNames_2 = FieldName[idx_2];

use table1;
read all var refNames into Y;
close;

/******Create 3x3 EAD transition matrix for each row of table 1*****/

do i = 1 to nrow(Y); /*I want to include the loop*/

TR_MAT = j(3,3,0);
TR_MAT[idx_2] = Y[i,];  /*this should be Y[i,]*/
temp = J(3,1)- TR_MAT[,+];
 do j=1 to 3;
 TR_MAT[j,j] = temp[j,1];
 end;
 
EAD_INIT = {10, 20, 33};

EAD_flows=(EAD_INIT#TR_MAT);
/**********write elements of EAD flow out*********/

EAD_flow_MAT = EAD_flows[idx_2];
EAD_flow_MAT = t(EAD_flow_MAT);	
new_EAD_flow_MAT=new_EAD_flow_MAT//EAD_flow_MAT;

end;
mattrib new_EAD_flow_MAT colname=refNames_2;

create table2 from new_EAD_flow_MAT[colname = refNames_2];
append from new_EAD_flow_MAT; 
close table2;

quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 03 Jan 2018 13:37:03 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2018-01-03T13:37:03Z</dc:date>
    <item>
      <title>Writing to a data table from a matrix within a do loop  - iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Writing-to-a-data-table-from-a-matrix-within-a-do-loop-iml/m-p/424357#M3954</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a parametrisation table and a data table. For each row of data table, I am creating a transition matrix, multiplying that matrix to a constant matrix element-wise and trying to write specific elements of output matrix to a new table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The following code works perfectly without the do loop, i.e. it only works for one row of table1 at a time. The writing ("create" and "append" statement within a do loop) does not work when I have the do loop. Ideally I want for one run of the loop a new row will be appended to the table2 that I am creating.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;/********create parametrisation table*********/&lt;BR /&gt;&lt;BR /&gt;data param_table;&lt;BR /&gt;infile datalines dsd;&lt;BR /&gt;length FieldName $20 FieldSourceTable $20;&lt;BR /&gt;input Matrix_Id Row_Order Column_Order IsZero IsRowResidual IsColumnResidual FieldName FieldSourceTable;&lt;BR /&gt;datalines;&lt;BR /&gt;1, 1, 1, 0, 1, 0, ., .&lt;BR /&gt;1, 1, 2, 0, 0, 0, xyz, table1&lt;BR /&gt;1, 1, 3, 0, 0, 0, abc, table1&lt;BR /&gt;1, 2, 1, 1, 0, 0, ., .&lt;BR /&gt;1, 2, 2, 0, 0, 0, pqr, table1&lt;BR /&gt;1, 2, 3, 0, 0, 0, mno, table1&lt;BR /&gt;1, 3, 1, 0, 0, 0, ab, table1&lt;BR /&gt;1, 3, 2, 0, 0, 0, pq, table1&lt;BR /&gt;1, 3, 3, 0, 1, 0, ., .&lt;BR /&gt;2, 1, 1, 1, 0, 0, ., .&lt;BR /&gt;2, 1, 2, 0, 0, 0, result1, table2&lt;BR /&gt;2, 1, 3, 0, 0, 0, result2, table2&lt;BR /&gt;2, 2, 1, 1, 0, 0, ., .&lt;BR /&gt;2, 2, 2, 0, 0, 0, sum1, table2&lt;BR /&gt;2, 2, 3, 0, 0, 0, sum2, table2&lt;BR /&gt;2, 3, 1, 0, 0, 0, prod1, table2&lt;BR /&gt;2, 3, 2, 0, 0, 0, prod2, table2&lt;BR /&gt;2, 3, 3, 1, 0, 0, ., .&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;proc sort data= param_table out= param_table;&lt;BR /&gt;by Matrix_Id Row_Order Column_Order;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table param_tr as&lt;BR /&gt;select * from work.param_table&lt;BR /&gt;where Matrix_Id = 1;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table param_flow as&lt;BR /&gt;select * from work.param_table&lt;BR /&gt;where Matrix_Id = 2;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;/********create data table*********/&lt;BR /&gt;&lt;BR /&gt;data table1;&lt;BR /&gt;input Year (country method Segment) ( : $12.)&lt;BR /&gt; ABC XYZ PQR MNO AB PQ;&lt;BR /&gt;datalines;&lt;BR /&gt;2017 France ABC Retail 0.2 0.5 0.4 0.3 0.6 0.1&lt;BR /&gt;2017 France XYZ Corporate 0.1 0.5 0.4 0.2 0.6 0.2&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;/*********build matrices and write out*********/&lt;BR /&gt;&lt;BR /&gt;proc iml;&lt;BR /&gt;use param_tr; &lt;BR /&gt;read all var {Column_Order Row_Order FieldName};&lt;BR /&gt;close;&lt;BR /&gt;&lt;BR /&gt;idx = loc(FieldName ^= " ");&lt;BR /&gt;refNames = FieldName[idx];&lt;BR /&gt;&lt;BR /&gt;use param_flow; &lt;BR /&gt;read all var {Column_Order Row_Order FieldName};&lt;BR /&gt;close;&lt;BR /&gt;&lt;BR /&gt;idx_2 = loc(FieldName ^= " ");&lt;BR /&gt;refNames_2 = FieldName[idx_2];&lt;BR /&gt;&lt;BR /&gt;use table1;&lt;BR /&gt;read all var refNames into Y;&lt;BR /&gt;close;&lt;BR /&gt;&lt;BR /&gt;/******Create 3x3 EAD transition matrix for each row of table 1*****/&lt;BR /&gt;&lt;BR /&gt;/*do i = 1 to nrow(Y);*/ /*I want to include the loop*/&lt;BR /&gt;&lt;BR /&gt;TR_MAT = j(3,3,0);&lt;BR /&gt;TR_MAT[idx_2] = Y[2,];  /*this should be Y[i,]*/&lt;BR /&gt;temp = J(3,1)- TR_MAT[,+];&lt;BR /&gt; do j=1 to 3;&lt;BR /&gt; TR_MAT[j,j] = temp[j,1];&lt;BR /&gt; end;&lt;BR /&gt; &lt;BR /&gt;EAD_INIT = {10, 20, 33};&lt;BR /&gt;&lt;BR /&gt;EAD_flows=(EAD_INIT#TR_MAT);&lt;BR /&gt;&lt;BR /&gt;/**********write elements of EAD flow out*********/&lt;BR /&gt;&lt;BR /&gt;EAD_flow_MAT = EAD_flows[idx_2];&lt;BR /&gt;EAD_flow_MAT = t(EAD_flow_MAT);&lt;BR /&gt;mattrib EAD_flow_MAT colname=refNames_2;&lt;BR /&gt;&lt;BR /&gt;create table2 from EAD_flow_MAT[colname = refNames_2];&lt;BR /&gt;append from EAD_flow_MAT; /** create data set **/&lt;BR /&gt;close table2;&lt;BR /&gt;&lt;BR /&gt;/*end;*/ *******I want to have the do loop&lt;/PRE&gt;&lt;P&gt;How can I write the result of each row table1 to different rows of "table2" within a loop? Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jan 2018 16:32:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Writing-to-a-data-table-from-a-matrix-within-a-do-loop-iml/m-p/424357#M3954</guid>
      <dc:creator>ss59</dc:creator>
      <dc:date>2018-01-02T16:32:11Z</dc:date>
    </item>
    <item>
      <title>Re: Writing to a data table from a matrix within a do loop  - iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Writing-to-a-data-table-from-a-matrix-within-a-do-loop-iml/m-p/424362#M3955</link>
      <description>&lt;P&gt;Your code has errors....is that what you're trying to resolve?&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jan 2018 16:32:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Writing-to-a-data-table-from-a-matrix-within-a-do-loop-iml/m-p/424362#M3955</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-01-02T16:32:09Z</dc:date>
    </item>
    <item>
      <title>Re: Writing to a data table from a matrix within a do loop  - iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Writing-to-a-data-table-from-a-matrix-within-a-do-loop-iml/m-p/424366#M3956</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;this code runs in my machine in the current form. Could you please clarify what error you are getting?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to resolve this section:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;create table2 from EAD_flow_MAT[colname = refNames_2];&lt;BR /&gt;append from EAD_flow_MAT; /** create data set **/&lt;BR /&gt;close table2;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want have the do loop, create the dataset &lt;STRONG&gt;table2&lt;/STRONG&gt; in the first go of the loop, and then append to it in each subsequent steps of he do loop.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please let me know if that was not clear.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jan 2018 16:41:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Writing-to-a-data-table-from-a-matrix-within-a-do-loop-iml/m-p/424366#M3956</guid>
      <dc:creator>ss59</dc:creator>
      <dc:date>2018-01-02T16:41:19Z</dc:date>
    </item>
    <item>
      <title>Re: Writing to a data table from a matrix within a do loop  - iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Writing-to-a-data-table-from-a-matrix-within-a-do-loop-iml/m-p/424367#M3957</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;when I specify i =1 or 2, I get the desired output in dataset table2. I am trying to have the dataset open and append to it in each run of the loop and get as many rows as table1 as an end result.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jan 2018 16:43:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Writing-to-a-data-table-from-a-matrix-within-a-do-loop-iml/m-p/424367#M3957</guid>
      <dc:creator>ss59</dc:creator>
      <dc:date>2018-01-02T16:43:24Z</dc:date>
    </item>
    <item>
      <title>Re: Writing to a data table from a matrix within a do loop  - iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Writing-to-a-data-table-from-a-matrix-within-a-do-loop-iml/m-p/424368#M3958</link>
      <description>&lt;P&gt;Your modified code runs fine, the original did not.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jan 2018 16:43:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Writing-to-a-data-table-from-a-matrix-within-a-do-loop-iml/m-p/424368#M3958</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-01-02T16:43:37Z</dc:date>
    </item>
    <item>
      <title>Re: Writing to a data table from a matrix within a do loop  - iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Writing-to-a-data-table-from-a-matrix-within-a-do-loop-iml/m-p/424373#M3959</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;yes, sorry about that. But even the modified code does not serve my purpose. I want something like this (and the code below does not work) :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/********create parametrisation table*********/

data param_table;
infile datalines dsd;
length FieldName $20 FieldSourceTable $20;
input Matrix_Id Row_Order Column_Order IsZero IsRowResidual IsColumnResidual FieldName FieldSourceTable;
datalines;
1, 1, 1, 0, 1, 0, ., .
1, 1, 2, 0, 0, 0, xyz, table1
1, 1, 3, 0, 0, 0, abc, table1
1, 2, 1, 1, 0, 0, ., .
1, 2, 2, 0, 0, 0, pqr, table1
1, 2, 3, 0, 0, 0, mno, table1
1, 3, 1, 0, 0, 0, ab, table1
1, 3, 2, 0, 0, 0, pq, table1
1, 3, 3, 0, 1, 0, ., .
2, 1, 1, 1, 0, 0, ., .
2, 1, 2, 0, 0, 0, result1, table2
2, 1, 3, 0, 0, 0, result2, table2
2, 2, 1, 1, 0, 0, ., .
2, 2, 2, 0, 0, 0, sum1, table2
2, 2, 3, 0, 0, 0, sum2, table2
2, 3, 1, 0, 0, 0, prod1, table2
2, 3, 2, 0, 0, 0, prod2, table2
2, 3, 3, 1, 0, 0, ., .
;

proc sort data= param_table out= param_table;
by Matrix_Id Row_Order Column_Order;
quit;

proc sql;
create table param_tr as
select * from work.param_table
where Matrix_Id = 1;
quit;

proc sql;
create table param_flow as
select * from work.param_table
where Matrix_Id = 2;
quit;


/********create data table*********/

data table1;
input Year (country method Segment) ( : $12.)
 ABC XYZ PQR MNO AB PQ;
datalines;
2017 France ABC Retail 0.2 0.5 0.4 0.3 0.6 0.1
2017 France XYZ Corporate 0.1 0.5 0.4 0.2 0.6 0.2
;
run;

/*********build matrices and write out*********/

proc iml;
use param_tr; 
read all var {Column_Order Row_Order FieldName};
close;

idx = loc(FieldName ^= " ");
refNames = FieldName[idx];

use param_flow; 
read all var {Column_Order Row_Order FieldName};
close;

idx_2 = loc(FieldName ^= " ");
refNames_2 = FieldName[idx_2];

use table1;
read all var refNames into Y;
close;

/******Create 3x3 EAD transition matrix for each row of table 1*****/

do i = 1 to nrow(Y);

TR_MAT = j(3,3,0);
TR_MAT[idx_2] = Y[i,]; 
temp = J(3,1)- TR_MAT[,+];
 do j=1 to 3;
 TR_MAT[j,j] = temp[j,1];
 end;
 
EAD_INIT = {10, 20, 33};

EAD_flows=(EAD_INIT#TR_MAT);

/**********write elements of EAD flow out*********/

EAD_flow_MAT = EAD_flows[idx_2];
EAD_flow_MAT = t(EAD_flow_MAT);
mattrib EAD_flow_MAT colname=refNames_2;

create table2 from EAD_flow_MAT[colname = refNames_2];
append from EAD_flow_MAT; /** create data set and append in each subsequent steps**/

end;

close table2;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Jan 2018 16:51:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Writing-to-a-data-table-from-a-matrix-within-a-do-loop-iml/m-p/424373#M3959</guid>
      <dc:creator>ss59</dc:creator>
      <dc:date>2018-01-02T16:51:44Z</dc:date>
    </item>
    <item>
      <title>Re: Writing to a data table from a matrix within a do loop  - iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Writing-to-a-data-table-from-a-matrix-within-a-do-loop-iml/m-p/424376#M3960</link>
      <description>&lt;P&gt;if you want to append data then&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;create table2 &lt;SPAN class="token keyword"&gt;from&lt;/SPAN&gt; EAD_flow_MAT&lt;SPAN class="token punctuation"&gt;[&lt;/SPAN&gt;colname &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; refNames_2&lt;SPAN class="token punctuation"&gt;]&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;has to be out of loop. otherwise you replacing that dataset then appending to the dataset&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jan 2018 17:18:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Writing-to-a-data-table-from-a-matrix-within-a-do-loop-iml/m-p/424376#M3960</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2018-01-02T17:18:31Z</dc:date>
    </item>
    <item>
      <title>Re: Writing to a data table from a matrix within a do loop  - iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Writing-to-a-data-table-from-a-matrix-within-a-do-loop-iml/m-p/424378#M3961</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37783"&gt;@kiranv_&lt;/a&gt;&amp;nbsp; - that results in only the last matrix to be written to the data. Only one row. The other matrices in the loop are not captured.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you do &lt;STRONG&gt;print EAD_flows;&amp;nbsp;&lt;/STRONG&gt;before the end; you'll see these two matrices. I want one row for each matrix in the table2 data through the loop.&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 face="Arial"&gt;&lt;STRONG&gt;EAD_flows&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;3&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;5&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;2&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;0&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;6&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;6&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;19.8&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;3.3&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;FONT face="Arial" color="#000000"&gt;9.9&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&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 face="Arial"&gt;&lt;STRONG&gt;EAD_flows&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;4&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;5&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;1&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;0&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;8&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;4&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;19.8&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;6.6&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;6.6&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Tue, 02 Jan 2018 17:29:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Writing-to-a-data-table-from-a-matrix-within-a-do-loop-iml/m-p/424378#M3961</guid>
      <dc:creator>ss59</dc:creator>
      <dc:date>2018-01-02T17:29:11Z</dc:date>
    </item>
    <item>
      <title>Re: Writing to a data table from a matrix within a do loop  - iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Writing-to-a-data-table-from-a-matrix-within-a-do-loop-iml/m-p/424383#M3962</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/182781"&gt;@ss59&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37783"&gt;@kiranv_&lt;/a&gt;&amp;nbsp; - that results in only the last matrix to be written to the data. Only one row. The other matrices in the loop are not captured.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you do &lt;STRONG&gt;print EAD_flows;&amp;nbsp;&lt;/STRONG&gt;before the end; you'll see these two matrices. I want one row for each matrix in the table2 data through the loop.&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 face="Arial"&gt;&lt;STRONG&gt;EAD_flows&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;3&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;5&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;2&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;0&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;6&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;6&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;19.8&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;3.3&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD&gt;
&lt;P&gt;&lt;FONT face="Arial" color="#000000"&gt;9.9&lt;/FONT&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&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 face="Arial"&gt;&lt;STRONG&gt;EAD_flows&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;4&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;5&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;1&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;0&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;8&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;4&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;19.8&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;6.6&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;FONT face="Arial" color="#000000"&gt;6.6&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;What does that mean?&amp;nbsp; &amp;nbsp;An observation in a dataset cannot be a "matrix", unless it was a matrix with only one row.&lt;/P&gt;
&lt;P&gt;Do you want to write multiple observations for each pass of the do loop?&amp;nbsp; Or do you want to just write one of the rows of the matrix?&amp;nbsp; if so which row?&lt;/P&gt;
&lt;P&gt;Do you want to reshape your matrix into a one row matrix and append that?&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jan 2018 18:06:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Writing-to-a-data-table-from-a-matrix-within-a-do-loop-iml/m-p/424383#M3962</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-01-02T18:06:50Z</dc:date>
    </item>
    <item>
      <title>Re: Writing to a data table from a matrix within a do loop  - iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Writing-to-a-data-table-from-a-matrix-within-a-do-loop-iml/m-p/424423#M3963</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37783"&gt;@kiranv_&lt;/a&gt;, if you look at the param_table for Matrix_ID 2, it looks like following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;FieldName&lt;/TD&gt;&lt;TD&gt;FieldSourceTable&lt;/TD&gt;&lt;TD&gt;Matrix_Id&lt;/TD&gt;&lt;TD&gt;Row_Order&lt;/TD&gt;&lt;TD&gt;Column_Order&lt;/TD&gt;&lt;TD&g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t;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;result1&lt;/TD&gt;&lt;TD&gt;result2&lt;/TD&gt;&lt;TD&gt;sum1&lt;/TD&gt;&lt;TD&gt;sum2&lt;/TD&gt;&lt;TD&gt;prod1&lt;/TD&gt;&lt;TD&gt;prod2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT color="#000000"&gt;5&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#000000"&gt;2&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#000000"&gt;6&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#000000"&gt;6&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#000000"&gt;19.8&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#000000"&gt;3.3&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT color="#000000"&gt;5&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#000000"&gt;1&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#000000"&gt;8&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#000000"&gt;4&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#000000"&gt;19.8&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#000000"&gt;6.6&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;The first row comes from the first EAD_flows matrix, second row comes form the second matrix (with desired elements as mentioned in param_table). I can do it for one row, but seeking help to do it in the loop so that I get the dataset &lt;STRONG&gt;table2 &lt;/STRONG&gt;with multiple rows corresponding to each Ead_flow matrix&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jan 2018 19:49:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Writing-to-a-data-table-from-a-matrix-within-a-do-loop-iml/m-p/424423#M3963</guid>
      <dc:creator>ss59</dc:creator>
      <dc:date>2018-01-02T19:49:10Z</dc:date>
    </item>
    <item>
      <title>Re: Writing to a data table from a matrix within a do loop  - iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Writing-to-a-data-table-from-a-matrix-within-a-do-loop-iml/m-p/424559#M3964</link>
      <description>&lt;P&gt;How about :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/********create parametrisation table*********/

data param_table;
infile datalines dsd;
length FieldName $20 FieldSourceTable $20;
input Matrix_Id Row_Order Column_Order IsZero IsRowResidual IsColumnResidual FieldName FieldSourceTable;
datalines;
1, 1, 1, 0, 1, 0, ., .
1, 1, 2, 0, 0, 0, xyz, table1
1, 1, 3, 0, 0, 0, abc, table1
1, 2, 1, 1, 0, 0, ., .
1, 2, 2, 0, 0, 0, pqr, table1
1, 2, 3, 0, 0, 0, mno, table1
1, 3, 1, 0, 0, 0, ab, table1
1, 3, 2, 0, 0, 0, pq, table1
1, 3, 3, 0, 1, 0, ., .
2, 1, 1, 1, 0, 0, ., .
2, 1, 2, 0, 0, 0, result1, table2
2, 1, 3, 0, 0, 0, result2, table2
2, 2, 1, 1, 0, 0, ., .
2, 2, 2, 0, 0, 0, sum1, table2
2, 2, 3, 0, 0, 0, sum2, table2
2, 3, 1, 0, 0, 0, prod1, table2
2, 3, 2, 0, 0, 0, prod2, table2
2, 3, 3, 1, 0, 0, ., .
;

proc sort data= param_table out= param_table;
by Matrix_Id Row_Order Column_Order;
quit;

proc sql;
create table param_tr as
select * from work.param_table
where Matrix_Id = 1;
quit;

proc sql;
create table param_flow as
select * from work.param_table
where Matrix_Id = 2;
quit;


/********create data table*********/

data table1;
input Year (country method Segment) ( : $12.)
 ABC XYZ PQR MNO AB PQ;
datalines;
2017 France ABC Retail 0.2 0.5 0.4 0.3 0.6 0.1
2017 France XYZ Corporate 0.1 0.5 0.4 0.2 0.6 0.2
;
run;

/*********build matrices and write out*********/

proc iml;
use param_tr; 
read all var {Column_Order Row_Order FieldName};
close;

idx = loc(FieldName ^= " ");
refNames = FieldName[idx];

use param_flow; 
read all var {Column_Order Row_Order FieldName};
close;

idx_2 = loc(FieldName ^= " ");
refNames_2 = FieldName[idx_2];

use table1;
read all var refNames into Y;
close;

/******Create 3x3 EAD transition matrix for each row of table 1*****/

do i = 1 to nrow(Y); /*I want to include the loop*/

TR_MAT = j(3,3,0);
TR_MAT[idx_2] = Y[i,];  /*this should be Y[i,]*/
temp = J(3,1)- TR_MAT[,+];
 do j=1 to 3;
 TR_MAT[j,j] = temp[j,1];
 end;
 
EAD_INIT = {10, 20, 33};

EAD_flows=(EAD_INIT#TR_MAT);
/**********write elements of EAD flow out*********/

EAD_flow_MAT = EAD_flows[idx_2];
EAD_flow_MAT = t(EAD_flow_MAT);	
new_EAD_flow_MAT=new_EAD_flow_MAT//EAD_flow_MAT;

end;
mattrib new_EAD_flow_MAT colname=refNames_2;

create table2 from new_EAD_flow_MAT[colname = refNames_2];
append from new_EAD_flow_MAT; 
close table2;

quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Jan 2018 13:37:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Writing-to-a-data-table-from-a-matrix-within-a-do-loop-iml/m-p/424559#M3964</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-01-03T13:37:03Z</dc:date>
    </item>
  </channel>
</rss>

