<?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 Populate a matrix with an existing SAS dataset in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populate-a-matrix-with-an-existing-SAS-dataset/m-p/64891#M401</link>
    <description>Hi All,&lt;BR /&gt;
&lt;BR /&gt;
I have the following SAS dataset:&lt;BR /&gt;
&lt;BR /&gt;
row column element&lt;BR /&gt;
1     1           0.5&lt;BR /&gt;
1     3           0.2&lt;BR /&gt;
2     1           0.4&lt;BR /&gt;
2     2           0.6&lt;BR /&gt;
3     2           0.7&lt;BR /&gt;
3     3           0.1&lt;BR /&gt;
&lt;BR /&gt;
I want to convert this dataset to a matrix like this:&lt;BR /&gt;
&lt;BR /&gt;
A=&lt;BR /&gt;
0.5   0     0.2&lt;BR /&gt;
0.4   0.6   0&lt;BR /&gt;
0     0.7   0.1&lt;BR /&gt;
&lt;BR /&gt;
In other words, variable "row" represents the row number while variable "column" represents the column number for the corresponding "element" value. If row or column information is missing, then that element is set to be zero. How can I do this using PROC IML? Thanks.</description>
    <pubDate>Wed, 11 May 2011 15:26:15 GMT</pubDate>
    <dc:creator>richard_hu2003</dc:creator>
    <dc:date>2011-05-11T15:26:15Z</dc:date>
    <item>
      <title>Populate a matrix with an existing SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populate-a-matrix-with-an-existing-SAS-dataset/m-p/64891#M401</link>
      <description>Hi All,&lt;BR /&gt;
&lt;BR /&gt;
I have the following SAS dataset:&lt;BR /&gt;
&lt;BR /&gt;
row column element&lt;BR /&gt;
1     1           0.5&lt;BR /&gt;
1     3           0.2&lt;BR /&gt;
2     1           0.4&lt;BR /&gt;
2     2           0.6&lt;BR /&gt;
3     2           0.7&lt;BR /&gt;
3     3           0.1&lt;BR /&gt;
&lt;BR /&gt;
I want to convert this dataset to a matrix like this:&lt;BR /&gt;
&lt;BR /&gt;
A=&lt;BR /&gt;
0.5   0     0.2&lt;BR /&gt;
0.4   0.6   0&lt;BR /&gt;
0     0.7   0.1&lt;BR /&gt;
&lt;BR /&gt;
In other words, variable "row" represents the row number while variable "column" represents the column number for the corresponding "element" value. If row or column information is missing, then that element is set to be zero. How can I do this using PROC IML? Thanks.</description>
      <pubDate>Wed, 11 May 2011 15:26:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populate-a-matrix-with-an-existing-SAS-dataset/m-p/64891#M401</guid>
      <dc:creator>richard_hu2003</dc:creator>
      <dc:date>2011-05-11T15:26:15Z</dc:date>
    </item>
    <item>
      <title>Re: Populate a matrix with an existing SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populate-a-matrix-with-an-existing-SAS-dataset/m-p/64892#M402</link>
      <description>If you know the dimensions of the matrix ahead of time (3 x 3 for your example),&lt;BR /&gt;
you can just say:&lt;BR /&gt;
&lt;BR /&gt;
use MyData;&lt;BR /&gt;
read all var {element};&lt;BR /&gt;
A = shape(element, 3, 3);&lt;BR /&gt;
&lt;BR /&gt;
If the number of rows and columns are unknown, then:&lt;BR /&gt;
&lt;BR /&gt;
read all var {row col element};&lt;BR /&gt;
A = shape(element, max(row), max(col));</description>
      <pubDate>Wed, 11 May 2011 15:32:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populate-a-matrix-with-an-existing-SAS-dataset/m-p/64892#M402</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2011-05-11T15:32:58Z</dc:date>
    </item>
    <item>
      <title>Re: Populate a matrix with an existing SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populate-a-matrix-with-an-existing-SAS-dataset/m-p/64893#M403</link>
      <description>Hi Rick,&lt;BR /&gt;
&lt;BR /&gt;
Thanks for your quick reply. Actually, I just revised my question. Some elements are zero if row or column information is missing. Is there any way to handle those zero elements? Thanks.</description>
      <pubDate>Wed, 11 May 2011 15:48:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populate-a-matrix-with-an-existing-SAS-dataset/m-p/64893#M403</guid>
      <dc:creator>richard_hu2003</dc:creator>
      <dc:date>2011-05-11T15:48:23Z</dc:date>
    </item>
    <item>
      <title>Re: Populate a matrix with an existing SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populate-a-matrix-with-an-existing-SAS-dataset/m-p/64894#M404</link>
      <description>Sure. In that case you'll have to know (somehow) the size of the matrix in which you want to store the data. Let's say that you know numRow and numCol.&lt;BR /&gt;
&lt;BR /&gt;
I haven't tested (or even run) the following statements, so forgive any typos.&lt;BR /&gt;
&lt;BR /&gt;
If you are using SAS/IML 9.22, you can use the FULL function:&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/imlug/63541/HTML/default/viewer.htm#imlug_langref_sect098.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/imlug/63541/HTML/default/viewer.htm#imlug_langref_sect098.htm&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
read all var {element row col} into s;&lt;BR /&gt;
A = full(s);&lt;BR /&gt;
&lt;BR /&gt;
If you aren't using SAS/IML 9.22, then allocate A and then fill the nonzero elements by using the sub2ind module that I wrote about on my IML blog a few months ago:&lt;BR /&gt;
&lt;A href="http://blogs.sas.com/iml/index.php?/archives/86-Converting-Matrix-Subscripts-to-Indices.html" target="_blank"&gt;http://blogs.sas.com/iml/index.php?/archives/86-Converting-Matrix-Subscripts-to-Indices.html&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
read all var {element row col};&lt;BR /&gt;
A = j(numRow, numCol, 0);&lt;BR /&gt;
idx = sub2ind(numCol, row||col);&lt;BR /&gt;
A[ idx ] = element;&lt;BR /&gt;
&lt;BR /&gt;
(You *are* reading my blog, right? &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; )</description>
      <pubDate>Wed, 11 May 2011 16:40:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Populate-a-matrix-with-an-existing-SAS-dataset/m-p/64894#M404</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2011-05-11T16:40:20Z</dc:date>
    </item>
  </channel>
</rss>

