2

Writing data from a matrix to a SAS data set

 1 year ago
source link: https://blogs.sas.com/content/iml/2011/04/18/writing-data-from-a-matrix-to-a-sas-data-set.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

In a previous post, I showed how to read data from a SAS data set into SAS/IML matrices or vectors. This article shows the converse: how to use the CREATE, APPEND, and CLOSE statements to create a SAS data set from data stored in a matrix or in vectors.

Creating a SAS Data Set from SAS/IML Vectors

You can use the CREATE and APPEND statements to write a SAS data set from SAS/IML vectors. The following statements create three different SAS/IML vectors. The data in those vectors are then written to a data set called MyData in the Work library:

proc iml;
/** create SAS data set from vectors **/
y = {1,0,3,2,0,3}; /** 6 x 1 vector **/
z = {8,7,6,5,6};   /** 5 x 1 vector **/
c = {A A A B B B}; /** 1 x 6 character vector **/
 
create MyData var {y z c}; /** create data set **/
append;       /** write data in vectors **/
close MyData; /** close the data set **/

The CREATE statement opens Work.MyData for writing. The program creates the numerical vectors y and z, and the character vector c. The APPEND statement writes the values of the vectors listed on the VAR clause of the CREATE statement. The numeric vectors create numeric variables; the character vector creates a character variable, which in this case has length 1. The names of the variables in the data set are y, z, and c, which are the same names as the SAS/IML vectors.

The CLOSE statement closes the data set. It is a good programming practice to close data sets when you are finished writing the data.

Notice that it does not matter if a vector is a row vector or a column vector: each element of a vector is written to an observation in the data set. If the vectors do not contain the same number of elements, then the variables that result from the shorter vectors are padded with missing values. For example, the last observation for the z variable contains a missing value, as shown by the following call to PROC PRINT:

proc print data=MyData; run;

t_readdata.png

Creating a SAS Data Set from a SAS/IML Matrix

Use the FROM clause on the CREATE and APPEND statements to create a data set from a matrix. You can specify names for the data set variables by using the COLNAME= option to the FROM clause, as shown in the following example:

proc iml;
/** create SAS data set from a matrix **/
x = {1 2 3,
     4 5 6,
     7 8 9,
     3 2 1 }; /** 4 x 3 matrix **/
create MyData2 from x[colname={"q" "r" "s"}];
append from x;
close MyData2;

For this example, the names of the variables in the data set are q, r, and s. If you do not explicitly specify names for the data set variables, the variables are named COL1, COL2, and so forth.

proc print data=MyData2; run;

t_readdata2.png

For more details on reading and writing data in the SAS/IML language, see Chapter 3 of Statistical Programming with SAS/IML Software.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK