The SAS/IML READ statement has a few convenient features for reading data from SAS data sets. One is that you can read all variables into vectors of the same names by using the _ALL_ keyword. The following DATA steps create a data set called Mixed that contains three numeric and three character variables. PROC IML then reads those variables into vectors:
data Mixed; x=1; y=2; z=3; a='A'; b='B'; c='C'; run; proc iml; use Mixed; read all var _ALL_; close Mixed; show names;
Notice that the SAS/IML vectors that are created are named x, y, z, a, b, and c—the same names as the variables in the Mixed data set. As mentioned in a previous article, the _ALL_ keyword (in combination with the KEEP and DROP options) provides a powerful way to read data.
However, you need to be aware of a special behavior when you use the _ALL_ keyword in conjunction with the INTO clause. The INTO clause reads variables into a matrix. Recall that a matrix is either character or numeric. You cannot have a matrix that has some numerical elements and some character elements. Consequently, what happens in the following example?
/* what happens if you use the INTO clause? */ use Mixed; read all var _ALL_ into A[colname=varNames]; close Mixed; print varNames;
As shown in the table, SAS/IML reads the numeric variables into the matrix A. The character variables in the Mixed data set are dropped.
You can read the character variables into a matrix, but you need to use a different keyword. The SAS/IML language supports the _NUM_ and _CHAR_ keywords in order to read all numeric and all character variables (respectively) into a matrix, as shown in the following example:
use Mixed; read all var _NUM_ into X[colname=NumerNames]; read all var _CHAR_ into C[colname=CharNames]; close; print NumerNames, CharNames;
I always use the _NUM_ or _CHAR_ keyword in conjunction with the INTO statement. It makes the code clearer and easier to read. And there is never any question regarding the type of the matrices that result.