<?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 write a function to convert matrix into CSV in SAS? in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-to-write-a-function-to-convert-matrix-into-CSV-in-SAS/m-p/574986#M12774</link>
    <description>&lt;P&gt;Can you explain what that code is trying to do?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It does not seem to relate to the topic of create a CSV (comma separated values) file.&lt;/P&gt;
&lt;P&gt;Does CSV mean something else in this case?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the meaning of the two inputs?&lt;/P&gt;
&lt;P&gt;What output do you want for your example input?&lt;/P&gt;</description>
    <pubDate>Fri, 19 Jul 2019 18:51:55 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-07-19T18:51:55Z</dc:date>
    <item>
      <title>How to write a function to convert matrix into CSV in SAS?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-write-a-function-to-convert-matrix-into-CSV-in-SAS/m-p/574690#M12729</link>
      <description>&lt;P&gt;I have these two matrix called &lt;STRONG&gt;my_mat&lt;/STRONG&gt; and &lt;STRONG&gt;NATR332&lt;/STRONG&gt;. I would like to convert matrix into csv format which can be done by the code I have below and get results&amp;nbsp;&lt;STRONG&gt;my_mat_csv &lt;/STRONG&gt;and&lt;STRONG&gt;&amp;nbsp;&lt;SPAN&gt;NATR_csv&lt;/SPAN&gt;&lt;/STRONG&gt;. However, I am more interested in writing a function that does the exact same thing instead of looping over the two matrices two times. Coming from R, I somehow managed to write this loop below, but I am not familiar with functions in SAS so I would really appreciate any help you could provide me.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc iml;&lt;BR /&gt;/* lets create a matrix */&lt;/P&gt;&lt;P&gt;my_mat ={'a' 'b' 'c', 'c' 'd' 'e', 'f' 'g' 'h'};&lt;BR /&gt;mat_col = {"C1" "C2" "C3"};&lt;BR /&gt;print my_mat[colname=mat_col];&lt;BR /&gt;Y1 = {146 141 135 142 140 143 138 137 142 136};&lt;BR /&gt;Y2 = {141 143 139 139 140 141 138 140 142 138};&lt;BR /&gt;NATR332 = {};&lt;BR /&gt;length = dimension(Y1)[2];&lt;BR /&gt;nat_ColName={"Y1" "Y2"};&lt;BR /&gt;NATR332={};&lt;BR /&gt;do i=1 to length;&lt;BR /&gt;a=0;&lt;BR /&gt;a=y1[i];&lt;BR /&gt;b=y2[i];&lt;BR /&gt;NATR332=NATR332 // (a || b);&lt;BR /&gt;end;&lt;BR /&gt;print NATR332[colname=nat_ColName];&lt;BR /&gt;dimension_m= dimension(my_mat);&lt;BR /&gt;i=1;&lt;BR /&gt;j=1;&lt;BR /&gt;s="";&lt;BR /&gt;s=s+mat_col[1]+","+mat_col[2]+","+mat_col[3]+"\n,";&lt;BR /&gt;do i=1 to dimension_m[1];&lt;BR /&gt;do j=1 to dimension_m[2];&lt;BR /&gt;if(j^=dimension_m[2]) then s=s+my_mat[i,j]+",";&lt;BR /&gt;else if(j=dimension_m[2]) then&lt;BR /&gt;do;&lt;BR /&gt;if(i=dimension_m[1]) then&lt;BR /&gt;do;&lt;BR /&gt;s=s+my_mat[i,j];&lt;BR /&gt;LEAVE;&lt;BR /&gt;end;&lt;BR /&gt;else s=s+my_mat[i,j]+"\n,";&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;my_mat_csv=s;&lt;BR /&gt;print(my_mat_csv);&lt;BR /&gt;my_mat=NATR332;&lt;BR /&gt;dimension_m=dimension(my_mat);&lt;BR /&gt;i=1;&lt;BR /&gt;j=1;&lt;BR /&gt;s=nat_ColName[1]+","+nat_ColName[2]+"\n,";&lt;BR /&gt;do i=1 to dimension_m[1];&lt;BR /&gt;do j=1 to dimension_m[2];&lt;BR /&gt;val=putn(my_mat[i,j],"3.");&lt;BR /&gt;if(j^=dimension_m[2]) then s=s+val+",";&lt;BR /&gt;else if(j=dimension_m[2]) then&lt;BR /&gt;do;&lt;BR /&gt;if(i=dimension_m[1]) then&lt;BR /&gt;do;&lt;BR /&gt;s=s+val;&lt;BR /&gt;LEAVE;&lt;BR /&gt;end;&lt;BR /&gt;else s=s+val+"\n,";&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;NATR_csv=s;&lt;BR /&gt;print(NATR_csv);&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Matrix I am interested in testing the function is &lt;STRONG&gt;NATR332 &lt;/STRONG&gt;below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data NATR332;&lt;BR /&gt;input Y1 Y2;&lt;BR /&gt;datalines;&lt;BR /&gt;146 141&lt;BR /&gt;141 143&lt;BR /&gt;135 139&lt;BR /&gt;142 139&lt;BR /&gt;140 140&lt;BR /&gt;143 141&lt;BR /&gt;138 138&lt;BR /&gt;137 140&lt;BR /&gt;142 142&lt;BR /&gt;136 138&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jul 2019 17:47:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-write-a-function-to-convert-matrix-into-CSV-in-SAS/m-p/574690#M12729</guid>
      <dc:creator>mapk</dc:creator>
      <dc:date>2019-07-18T17:47:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a function to convert matrix into CSV in SAS?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-write-a-function-to-convert-matrix-into-CSV-in-SAS/m-p/574695#M12732</link>
      <description>Any reason to not write it to a data set and then use PROC EXPORT?&lt;BR /&gt;&lt;BR /&gt;Instructions on making a data set:&lt;BR /&gt;&lt;A href="https://blogs.sas.com/content/iml/2011/04/18/writing-data-from-a-matrix-to-a-sas-data-set.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2011/04/18/writing-data-from-a-matrix-to-a-sas-data-set.html&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;PROC EXPORT are pretty straighforward.&lt;BR /&gt;&lt;BR /&gt;proc export out=data2Export datafile="path to file to save it.csv" dbms=csv replace;run;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 18 Jul 2019 17:51:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-write-a-function-to-convert-matrix-into-CSV-in-SAS/m-p/574695#M12732</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-07-18T17:51:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a function to convert matrix into CSV in SAS?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-write-a-function-to-convert-matrix-into-CSV-in-SAS/m-p/574700#M12733</link>
      <description>I tried working with the dataset data NATR332, but couldn't loop over it.&lt;BR /&gt;</description>
      <pubDate>Thu, 18 Jul 2019 18:01:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-write-a-function-to-convert-matrix-into-CSV-in-SAS/m-p/574700#M12733</guid>
      <dc:creator>mapk</dc:creator>
      <dc:date>2019-07-18T18:01:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a function to convert matrix into CSV in SAS?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-write-a-function-to-convert-matrix-into-CSV-in-SAS/m-p/574701#M12734</link>
      <description>Also, I did not want to save it as a csv file. I wanted to print it to the console.</description>
      <pubDate>Thu, 18 Jul 2019 18:04:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-write-a-function-to-convert-matrix-into-CSV-in-SAS/m-p/574701#M12734</guid>
      <dc:creator>mapk</dc:creator>
      <dc:date>2019-07-18T18:04:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a function to convert matrix into CSV in SAS?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-write-a-function-to-convert-matrix-into-CSV-in-SAS/m-p/574740#M12738</link>
      <description>As a comma delimited output? Can I ask why you wouldn't want a formatted table? &lt;BR /&gt;SAS doesn't really use loops, the data step loops automatically so you don't have to do that, which is a great benefit and part of what makes it powerful. It does make it much harder to understand if you're familiar with Matlab, R or Python though. SAS is more like R and using tidyverse. &lt;BR /&gt;&lt;BR /&gt;Some questions:&lt;BR /&gt;&lt;BR /&gt;1. What's your ultimate objective?&lt;BR /&gt;2. Do you have to use IML? Or is it optional?&lt;BR /&gt;&lt;BR /&gt;Data steps and procs are usually a lot more efficient for most things.</description>
      <pubDate>Thu, 18 Jul 2019 19:23:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-write-a-function-to-convert-matrix-into-CSV-in-SAS/m-p/574740#M12738</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-07-18T19:23:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a function to convert matrix into CSV in SAS?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-write-a-function-to-convert-matrix-into-CSV-in-SAS/m-p/574933#M12757</link>
      <description>&lt;P&gt;You could try&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ODS CSV file='.......';&lt;/P&gt;
&lt;P&gt;proc iml;&lt;/P&gt;
&lt;P&gt;...........&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;or calling&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jul 2019 13:06:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-write-a-function-to-convert-matrix-into-CSV-in-SAS/m-p/574933#M12757</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-07-19T13:06:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a function to convert matrix into CSV in SAS?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-write-a-function-to-convert-matrix-into-CSV-in-SAS/m-p/574986#M12774</link>
      <description>&lt;P&gt;Can you explain what that code is trying to do?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It does not seem to relate to the topic of create a CSV (comma separated values) file.&lt;/P&gt;
&lt;P&gt;Does CSV mean something else in this case?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the meaning of the two inputs?&lt;/P&gt;
&lt;P&gt;What output do you want for your example input?&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jul 2019 18:51:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-write-a-function-to-convert-matrix-into-CSV-in-SAS/m-p/574986#M12774</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-19T18:51:55Z</dc:date>
    </item>
  </channel>
</rss>

