Using the Read_Array and Write_Array Functions in PROC FCMP

As I describe in the post Data Step Array Vs. PROC FCMP Array in SAS, the Array in PROC FCMP is quite different from the usual SAS Data Step Array. In the post, I list 6 differences between the two. Today, I investigate yet another distinction. Namely the ability to read and write data directly to/from the array. This is done with the Read_Array and Write_Array Functions respectively.

The Read_Array Function

The Read_Array Function has two required arguments. A SAS data set name in quotation and an array name. The array must already exist in PROC FCMP. Consider the small example below. Here, I create a small example data set in with the numeric variables a, b and c. Next, I use PROC FCMP and create a 3×3 matrix. The /Nosymbols syntax is similar to the _temporary_ option in the SAS data step. Next, I use the Read_Array Function to read the data set in into the array x. Since Read_Array is a function, we have to use it in an assign statement. I specify rc to hold the returned value, which is zero for a successful operation. You can see in the output, that the result is as desired.

data in;
input a b c;
datalines;
1 2 3
4 5 6
7 8 9
;
 
proc fcmp;
   array x{3, 3} / nosymbols;
 
   rc=read_array('in',x);
   put x=;
quit;

As I describe in the blog post in the top of this article, the size of a PROC FCMP temporary array is dynamic. Unlike a data step array, it can change dimensions and even number of dimensions. Consequently, we do not have to explicitly specify the size of the array before we read the array. SAS can handle it internally. See the code below for an example. Here, I simply define x with a single dimension of 1. The result is the same as above.

proc fcmp;
   array x{1} / nosymbols;
 
   rc=read_array('in',x);
 
   dim1=dim(x, 1);
   dim2=dim(x, 2);
 
   put dim1= / dim2=;
quit;

We do not have to read the entire data set into the SAS array. We can specify specific columns in the function call. In the code below, we only read in a and b.

proc fcmp;
   array x{1} / nosymbols;
 
   rc=read_array('in', x, 'a', 'b');
   put x=;
quit;

The Write_Array Function

Next, let us consider the Write_Array Function. Just like the Read_Array Function above, it has two required arguments. A SAS data set name in quotation and an array name. The code below creates a 3×3 matrix x within PROC FCMP. Next, it writes the array to a data set out. The out data set has three variables x1, x2 and x3 and three observations.

proc fcmp;
   array x{3, 3} / nosymbols ( 1 2 3,
                               4 5 6,
                               7 8 9 );
   rc=write_array('out', x);
quit;

As a final example, let us put the two function together. In the code below, I use the Call Transpose Routine to transpose the matrix read by the Read_Array Function. Finally, I use the Write_Array Function to write the transposed data back into the out data set. I use the optional arguments ‘a’, ‘b’ and ‘c’ to specify the column names of the written matrix.

data in;
input a b c;
datalines;
1 2 3
4 5 6
7 8 9
;
 
proc fcmp;
   array x {3, 3} / nosymbols;
   array y {3, 3} / nosymbols;
 
   rc=read_array('in',x);
   call transpose(x, y);
   rc=write_array('out', y, 'a', 'b', 'c');
quit;

Summary

In this post, I have investigated the two SAS functions Read_Array and Write_Array. The two functions are specific to PROC FCMP and do not work in the data step. We have seen a few code examples and seen how the function exploit the FCMP arrays’ ability to grow and shrink. This is also specific to the FCMP array.

For other PROC FCMP related posts, see How Are PROC FCMP Functions Stored in SAS? and Use SAS Hash Object in PROC FCMP.

You can download the entire code from this post here.