Statistical programming, matrix languages, and more

define a module to read different variable in one dataset

Accepted Solution Solved
Reply
Contributor
Posts: 44
Accepted Solution

define a module to read different variable in one dataset

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.


Accepted Solutions
Solution
‎04-24-2015 07:55 AM
SAS Super FREQ
Posts: 3,416

Re: define a module to read different variable in one dataset

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


All Replies
Solution
‎04-24-2015 07:55 AM
SAS Super FREQ
Posts: 3,416

Re: define a module to read different variable in one dataset

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

Contributor
Posts: 44

Re: define a module to read different variable in one dataset

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?

SAS Super FREQ
Posts: 3,416

Re: define a module to read different variable in one dataset

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

Contributor
Posts: 44

Re: define a module to read different variable in one dataset

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?

SAS Super FREQ
Posts: 3,416

Re: define a module to read different variable in one dataset

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

Grand Advisor
Posts: 9,593

Re: define a module to read different variable in one dataset

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

Contributor
Posts: 44

Re: define a module to read different variable in one dataset

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

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 559 views
  • 4 likes
  • 3 in conversation