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:
- Use the CREATE and APPEND statement to write data from vectors (of any types) to a SAS data set.
- Put the data into a SAS/IML table (introduced in SAS/IML 14.2 as part of SAS 9.4m4) and use the TableWriteToDataset call to write the data set.
- Use the CREATE FROM and APPEND FROM statements to write two data sets: one that contains all the numeric data and another that contains the character data. Use the DATA step to merge the data sets.
- New in SAS/IML 15.1: Use the CREATE FROM statement and APPEND FROM statement to write multiple matrices, as shown in the following section.
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.
2 Comments
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!
The article states multiple times that this feature is available only in SAS/IML 15.1 or later (SAS 9.4m6 or later).