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

2

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 SAS/IML software. 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.

2 Comments

  1. Rick,
    the code above produces an error:

    create My_Data from X C [colname=AllNames];
    -
    22
    202
    ERROR 22-322: Syntax error, expecting one of the following: ;, (|, [.
    ERROR 202-322: The option or parameter is not recognized and will be ignored.
    append from X C;
    -
    22
    76
    ERROR 22-322: Syntax error, expecting one of the following: FROM, VAR, VARIABLES.
    ERROR 76-322: Syntax error, statement will be ignored.

    please comment. many thanks in advance!

Leave A Reply

Back to Top