<?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 do I preserve column names when returning a matrix from a function in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-do-I-preserve-column-names-when-returning-a-matrix-from-a/m-p/749294#M5492</link>
    <description>&lt;P&gt;For an introduction to lists, see &lt;A href="https://blogs.sas.com/content/iml/2017/03/29/lists-sasiml.html" target="_self"&gt;"Lists: Nonmatrix data structures in SAS/IML"&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;For an introduction to the list notational syntax, see &lt;A href="https://blogs.sas.com/content/iml/2018/01/22/iml-lists-syntax.html" target="_self"&gt;"Create lists by using a natural syntax in SAS/IML"&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 21 Jun 2021 15:52:55 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2021-06-21T15:52:55Z</dc:date>
    <item>
      <title>how do I preserve column names when returning a matrix from a function</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-do-I-preserve-column-names-when-returning-a-matrix-from-a/m-p/749270#M5489</link>
      <description>&lt;P&gt;The following little program assigns column names to a row vector mu_vec.&amp;nbsp; However, if this processing is carried out within a function, then the command "return(mu_vec);" will get rid of the column names.&amp;nbsp; Is there a way to preserve these column names?&amp;nbsp;&lt;/P&gt;&lt;PRE class="language-sas"&gt;&lt;CODE&gt;assetNames={"MSFT" "NORD" "SBUX"};
mu_vec={0.042 0.0015 0.028};
mattrib mu_vec c=assetNames;
print mu_vec;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Jun 2021 14:52:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-do-I-preserve-column-names-when-returning-a-matrix-from-a/m-p/749270#M5489</guid>
      <dc:creator>kgwet</dc:creator>
      <dc:date>2021-06-21T14:52:08Z</dc:date>
    </item>
    <item>
      <title>Re: how do I preserve column names when returning a matrix from a function</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-do-I-preserve-column-names-when-returning-a-matrix-from-a/m-p/749278#M5490</link>
      <description>&lt;P&gt;I assume that you are trying something like the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;start Func(x);
   assetNames={"MSFT" "NORD" "SBUX"};
   mu_vec={0.042 0.0015 0.028};
   mattrib mu_vec c=assetNames;
   return mu_vec;
finish;

y = Func(x);  /* copies values but not atrtributes */
print y;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This doesn't work the way you hoped because inside the FUNC module, the variable X is a local variable. Any attributes you assign as assigned to the local variable and are not copied to the variable Y, which has a different scope. (&lt;A href="https://blogs.sas.com/content/iml/2013/04/29/understanding-local-and-global-variables-in-the-sasiml-language.html" target="_self"&gt;See this article to learn more about the scope of variables in the SAS/IML language.&lt;/A&gt;)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to give Y attributes, you need to assign them in the scope for Y. If the column names are known inside the function, you can return them (as part of a list) from the function. You can then call the MATTRIB statement in the parent environment. like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;start Func2(x);
   assetNames={"MSFT" "NORD" "SBUX"};
   mu_vec={0.042 0.0015 0.028};
   return ( [mu_vec, assetNames] );  /* return a list */
finish;

L = Func2(x);      /* output is a list with two items */
y = L$1;           /* values are the first item in list */
mattrib y c=(L$2); /* column names are the second item */
print y;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Jun 2021 15:19:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-do-I-preserve-column-names-when-returning-a-matrix-from-a/m-p/749278#M5490</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2021-06-21T15:19:40Z</dc:date>
    </item>
    <item>
      <title>Re: how do I preserve column names when returning a matrix from a function</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-do-I-preserve-column-names-when-returning-a-matrix-from-a/m-p/749288#M5491</link>
      <description>I love this solution. I didn't know about the list feature.&lt;BR /&gt;Thanks a lot.</description>
      <pubDate>Mon, 21 Jun 2021 15:42:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-do-I-preserve-column-names-when-returning-a-matrix-from-a/m-p/749288#M5491</guid>
      <dc:creator>kgwet</dc:creator>
      <dc:date>2021-06-21T15:42:04Z</dc:date>
    </item>
    <item>
      <title>Re: how do I preserve column names when returning a matrix from a function</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-do-I-preserve-column-names-when-returning-a-matrix-from-a/m-p/749294#M5492</link>
      <description>&lt;P&gt;For an introduction to lists, see &lt;A href="https://blogs.sas.com/content/iml/2017/03/29/lists-sasiml.html" target="_self"&gt;"Lists: Nonmatrix data structures in SAS/IML"&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;For an introduction to the list notational syntax, see &lt;A href="https://blogs.sas.com/content/iml/2018/01/22/iml-lists-syntax.html" target="_self"&gt;"Create lists by using a natural syntax in SAS/IML"&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Jun 2021 15:52:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-do-I-preserve-column-names-when-returning-a-matrix-from-a/m-p/749294#M5492</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2021-06-21T15:52:55Z</dc:date>
    </item>
    <item>
      <title>Re: how do I preserve column names when returning a matrix from a function</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-do-I-preserve-column-names-when-returning-a-matrix-from-a/m-p/749512#M5493</link>
      <description>&lt;P&gt;Dr. Rick has said very clear.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or could pass local matrix into global matrix ,if your IML is low version.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;

start Func(x) global(mu ,asset );
   assetNames={"MSFT" "NORD" "SBUX"};
   mu_vec={0.042 0.0015 0.028};
   mattrib mu_vec c=assetNames;
   mu=mu_vec ;asset=assetNames;
   return mu_vec;
finish;

y = Func(x);  /* copies values but not atrtributes */
mattrib mu c=asset l='';
print y , mu ;

quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1624366596428.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/60608i23A9ED8E1835FAFB/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1624366596428.png" alt="Ksharp_0-1624366596428.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Jun 2021 12:56:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-do-I-preserve-column-names-when-returning-a-matrix-from-a/m-p/749512#M5493</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-06-22T12:56:42Z</dc:date>
    </item>
    <item>
      <title>Re: how do I preserve column names when returning a matrix from a function</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-do-I-preserve-column-names-when-returning-a-matrix-from-a/m-p/749532#M5494</link>
      <description>Thanks for your suggestion. Would global passing of a matrix overwrite a matrix of the same name in the parent program?</description>
      <pubDate>Tue, 22 Jun 2021 13:45:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-do-I-preserve-column-names-when-returning-a-matrix-from-a/m-p/749532#M5494</guid>
      <dc:creator>kgwet</dc:creator>
      <dc:date>2021-06-22T13:45:39Z</dc:date>
    </item>
    <item>
      <title>Re: how do I preserve column names when returning a matrix from a function</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-do-I-preserve-column-names-when-returning-a-matrix-from-a/m-p/749831#M5499</link>
      <description>Yes. It is global. It would  be overwrited if there are same name in parent program.&lt;BR /&gt;But you could define a unique name to avoid it .</description>
      <pubDate>Wed, 23 Jun 2021 11:41:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-do-I-preserve-column-names-when-returning-a-matrix-from-a/m-p/749831#M5499</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-06-23T11:41:34Z</dc:date>
    </item>
  </channel>
</rss>

