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;
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?
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.