## Creat matrix using SAS iml

I wanted to create matrix from my existing data set , partial code is as follows:

proc iml;

use all;

read all var { w1 w2 w3 w4} into w;

/* the problem here is I need to read each observation into matrix one at a time, but here it read all the obs into a large matrix, do I need to write a loop? How to do it?*/

use v1;

use v2;

if h=1 then r=w*v1; /* can I use if statement in iml, is it written right?*/

if h=2 then r=w*v2;

thank you so much!

5 REPLIES 5

## Re: Creat matrix using SAS iml

You refer to the variables h, v1, and v2, but these variables are not defined in your program.  As written, the program defined three matrices: w, var1, and var2.

You say that you "need to read each observation one at a time."  It is possible to do that, but it is usually more efficient to read all the data at once. Could you say more about why you feel that you need to read the data one obs at a time?

To read data one row at a time, use the DO DATA statement along with the NEXT clause of the READ statement, such as

READ NEXT k VAR _NUM_ into x;

For an example and discussion, see the article Sequential access: Reading one observation at a time in SAS/IML software - The DO Loop

## Re: Creat matrix using SAS iml

Thank you for your reply,Rick. I did define h in data all. v1, v2 as two separate data sets are defined as well. Sorry I did not give my full programming since it is too big. Sorry there is typo in my previous q, if h=1,r=w*var1 ,if h=2, r=w*var2

the reason I need to draw each obs ( representing one respondent of a survey ) into a matrix is:

my final goal is to compute a ratio s=(r-rf)/var, and use s as my dependent variable in my regression analysis.

In this situation ,i need to read each observation as a separate matrix, does it make sense? or can you offer other efficient way?

thanks a lot!

## Re: Creat matrix using SAS iml

HI, Rick,

if if I define variable h in dataset all, can I write : if h=1,r=w*var1 ,if h=2, r=w*var2, in iml procedure ?

thanks,

shine

## Re: Creat matrix using SAS iml

From your description, it looks like the values of H are contained in one of the columns of VAR1 because you used the INTO clause. However,  H is not defined as a variable unless you use

READ ALL VAR _NUM_;   /* create vectors for all numerical variables */

or

h = var1[,1]; /* if H is in the first column of var1 */

You can use SHOW NAMES to see what variables are defined.

After you have defined H, it sounds like you want to use the CHOOSE function to assign R based on the values of H.  See if the following statements gives you what you want:

h1 = w*var1;

h2 = w*var2;

r = choose(h=1, h1, h2);   /* when h=1, choose h1; otherwise choose h2 */

See the article Complex assignment statements: CHOOSE wisely - The DO Loop for more information about the CHOOSE function.

## Re: Creat matrix using SAS iml

Hi, I just try to answer your question. Before creating your matrix in SAS/Iml at all, you can create variable 'h' using in Data Step (Data Step is reading your data line by line too) , something like...

--------------------------

DATA New;

SET WORK.Old;

IF h=1 THEN r=w*var1;

ELSE IF h=2 THEN r=w*var1;

RUN;

-------------------

Then you can export your variable to IML creating the matrix.

From The DO Loop