Write numeric and character matrices to a data set from SAS/IML

0

In the SAS/IML language, a matrix contains data of one type: numeric or character. If you want to create a SAS data set that contains mixed-type data (numeric and character), SAS/IML 15.1 provides support to write multiple matrices to a data set by using a single statement. Specifically, the CREATE FROM and APPEND FROM statements now support writing multiple matrices of any types. SAS/IML 15.1 was released as part of SAS 9.4m6.

Write mixed-type data from SAS/IML objects

With the new enhancements to the CREATE FROM and APPEND FROM statements, you now have four ways to write mixed type data to a SAS data set:

Write multiple matrices to a data set

In SAS/IML 15.1, you can specify multiple matrices on the CREATE FROM statement. The matrices can be any type. In the following example, X matrix is a numeric matrix and C is a character matrix:

/* read numeric and character vars in one call */
proc iml;
NumerVarNames = {'N' 'N2' 'N3'};
X = { 1  2  3,
      2  4  6,
      3  6  9,
      4  8 12};
charVarNames = {'Animal' 'Flower'};
C = {'Rat'   'Iris', 
     'Pig'   'Rose',
     'Goat'  'Daisy', 
     'Duck'  'Lily'};
 
/* SAS/IML 15.1: write multiple matrices of any type to a SAS data sets */
AllNames = NumerVarNames || CharVarNames;
create MyData from X C [colname=AllNames];  /* specify multiple matrices */
append from X C;                            /* repeat matrix names */
close;
QUIT;
 
proc print data=MyData noobs;
run;

Although the new enhancements to the CREATE FROM and APPEND FROM statements enable you to write mixed-type data to a SAS data set, you can also write multiple matrices regardless of the types. For example, you can use the same technique to write multiple numeric matrices.

Notice that if you want to specify the names of the data set variables, you use a single COLNAME= option at the end of the CREATE FROM statement.

Share

About Author

Rick Wicklin

Distinguished Researcher in Computational Statistics

Rick Wicklin, PhD, is a distinguished researcher in computational statistics at SAS and is a principal developer of PROC IML and SAS/IML Studio. His areas of expertise include computational statistics, simulation, statistical graphics, and modern methods in statistical data analysis. Rick is author of the books Statistical Programming with SAS/IML Software and Simulating Data with SAS.

Leave A Reply

Back to Top