BookmarkSubscribeRSS Feed
mapk
Fluorite | Level 6

I have these two matrix called my_mat and NATR332. I would like to convert matrix into csv format which can be done by the code I have below and get results my_mat_csv and NATR_csv. 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. 

 

proc iml;
/* lets create a matrix */

my_mat ={'a' 'b' 'c', 'c' 'd' 'e', 'f' 'g' 'h'};
mat_col = {"C1" "C2" "C3"};
print my_mat[colname=mat_col];
Y1 = {146 141 135 142 140 143 138 137 142 136};
Y2 = {141 143 139 139 140 141 138 140 142 138};
NATR332 = {};
length = dimension(Y1)[2];
nat_ColName={"Y1" "Y2"};
NATR332={};
do i=1 to length;
a=0;
a=y1[i];
b=y2[i];
NATR332=NATR332 // (a || b);
end;
print NATR332[colname=nat_ColName];
dimension_m= dimension(my_mat);
i=1;
j=1;
s="";
s=s+mat_col[1]+","+mat_col[2]+","+mat_col[3]+"\n,";
do i=1 to dimension_m[1];
do j=1 to dimension_m[2];
if(j^=dimension_m[2]) then s=s+my_mat[i,j]+",";
else if(j=dimension_m[2]) then
do;
if(i=dimension_m[1]) then
do;
s=s+my_mat[i,j];
LEAVE;
end;
else s=s+my_mat[i,j]+"\n,";
end;
end;
end;
my_mat_csv=s;
print(my_mat_csv);
my_mat=NATR332;
dimension_m=dimension(my_mat);
i=1;
j=1;
s=nat_ColName[1]+","+nat_ColName[2]+"\n,";
do i=1 to dimension_m[1];
do j=1 to dimension_m[2];
val=putn(my_mat[i,j],"3.");
if(j^=dimension_m[2]) then s=s+val+",";
else if(j=dimension_m[2]) then
do;
if(i=dimension_m[1]) then
do;
s=s+val;
LEAVE;
end;
else s=s+val+"\n,";
end;
end;
end;
NATR_csv=s;
print(NATR_csv);
quit;

 

 

Matrix I am interested in testing the function is NATR332 below:

 

data NATR332;
input Y1 Y2;
datalines;
146 141
141 143
135 139
142 139
140 140
143 141
138 138
137 140
142 142
136 138
run;

6 REPLIES 6
Reeza
Super User
Any reason to not write it to a data set and then use PROC EXPORT?

Instructions on making a data set:
https://blogs.sas.com/content/iml/2011/04/18/writing-data-from-a-matrix-to-a-sas-data-set.html

PROC EXPORT are pretty straighforward.

proc export out=data2Export datafile="path to file to save it.csv" dbms=csv replace;run;

mapk
Fluorite | Level 6
I tried working with the dataset data NATR332, but couldn't loop over it.
mapk
Fluorite | Level 6
Also, I did not want to save it as a csv file. I wanted to print it to the console.
Reeza
Super User
As a comma delimited output? Can I ask why you wouldn't want a formatted table?
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.

Some questions:

1. What's your ultimate objective?
2. Do you have to use IML? Or is it optional?

Data steps and procs are usually a lot more efficient for most things.
Ksharp
Super User

You could try 

ODS CSV file='.......';

proc iml;

...........

 

 

or calling @Rick_SAS 

Tom
Super User Tom
Super User

Can you explain what that code is trying to do?

 

It does not seem to relate to the topic of create a CSV (comma separated values) file.

Does CSV mean something else in this case?

 

What is the meaning of the two inputs?

What output do you want for your example input?