BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Hanyu
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

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");

View solution in original post

7 REPLIES 7
Rick_SAS
SAS Super FREQ

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");

Hanyu
Fluorite | Level 6

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?

Rick_SAS
SAS Super FREQ

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");

Hanyu
Fluorite | Level 6

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?

Rick_SAS
SAS Super FREQ

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

Ksharp
Super User

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

Hanyu
Fluorite | Level 6

Thank you very much. I think read({name sex}) would also work.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 7 replies
  • 1623 views
  • 4 likes
  • 3 in conversation