I got an email asking the following question:
In the following program, I don't know how many variables are in the data set A. However, I do know that the variable names are X1–Xk for some value of k. How can I read them all into a SAS/IML matrix when I don't know the value of k?
data _null_; call symputx("k", ceil(20*ranuni(0))); /* random number 1-20 */ run; data a; /* create data set with k variables */ array x[&k]; do n = 1 to 5; /* 5 obs */ do i = 1 to &k; /* k vars */ x[i] = ceil(10*ranuni(1)); end; output; end; run; proc iml; use A; read all var ??? into m; /* ??? how do I read X1-Xk ?! */ close A;
proc iml; use A(keep=x:); read all var _ALL_ into m[colname=c]; close A; print m[colname=c];
The KEEP= statement drops any variables that you don't want; the _ALL_ keyword reads in all variables that remain. Consequently, the matrix m contains all variables that begin with the prefix "x" (assuming they are all numeric or all character).