Can we define a module which has variable names as arguments and reads the variables into a matrix? I tried the following code and it does not work.
Suppose the variable "a" is a variable in dataset "y" and I want to read a into matrix su. The reason I want to do this is that I have many variables in y and I want to repeat my estimation for different variable. Thank you in advance.
proc iml;
start read(s);
use y;
read all var{s} into su;
close y;
finish;
run read(a);
quit;
ERROR: s is not in the scope of variables for the data set.
When you put characters into curly braces, you are defining a vector of character values.
Thus {s} is equal to "s".
Your READ statement should be
read all var s into su; /* no braces */
which means "read the variables that are contained in the character vector s."
To make it work, you have to pass in a character matrix. In your example, you would call it as
run read("a"); /* notice the quotes on "a"; it is a string */
I also suggest that you make your function return the su matrix by using
return(su);
and calling the function as
X = read("a");
When you put characters into curly braces, you are defining a vector of character values.
Thus {s} is equal to "s".
Your READ statement should be
read all var s into su; /* no braces */
which means "read the variables that are contained in the character vector s."
To make it work, you have to pass in a character matrix. In your example, you would call it as
run read("a"); /* notice the quotes on "a"; it is a string */
I also suggest that you make your function return the su matrix by using
return(su);
and calling the function as
X = read("a");
Thank you Dr. May I ask one more question? I want to also create dataset according to arguments of a module. My attempt is the following
proc iml;
start store_est(a,b);
estimate=T(1:30);
std=T(1:30);
display=estimate||std;
create estimate_a_b from display;
append from display;
close estimate_a_b;
finish;
store module=store_est;
run store_est(s1,s2);
However, the code does not work. Is there a way to parametrize the dataset name in the module?
Yes. The basic operation is explained in this article http://blogs.sas.com/content/iml/2013/07/29/read-data-sets-array-names.html
Basically you want to construct the data set name as a string by concatenating a root name with the strings contains in a and b. The following statements create the data set "estimate_1_2":
proc iml;
start store_est(a,b);
estimate=T(1:30);
std=T(1:30);
display=estimate||std;
DSName = "estimate_" + a + "_" + b;
create (DSName) from display;
append from display;
close (DSName);
finish;
store module=store_est;
run store_est("1","2");
Thank you Dr. Regarding my first question, I am now trying to read two variables into one matrix. I am a bit struggling with this operation.
proc iml;
start read(s);
use y;
read all var s into su;
close y;
finish;
run read("a b");
quit;
If I define s to be something like "a b" or "a,b", sas say the variable a b is not in the dataset. Could you help me?
This is starting to turn into "please write my program for me." I think you would benefit from taking an hour or two to learn about how to create and use SAS/IML character vectors.
The Doc (free):SAS/IML(R) 13.1 User's Guide
A good book ($$): Statistical Programming with SAS/IML® Software
The book has a FREE excerpt that has information about elementary matrix operations such as you are asking:
http://www.sas.com/storefront/aux/en/spstatprogiml/63119_excerpt.pdf
Assign value to that parameter matrix :
proc iml; start read(s); use sashelp.class; read all var s into su; close sashelp.class; print su; finish; run read({'name' 'sex'}); quit;
Xia Keshan
Thank you very much. I think read({name sex}) would also work.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.