## proc iml

i was doing homework

this are the questions for your reference.

I was struggling with q2 and q3

1) Read only the following variables into a matrix called CG: NACZZMS, NACCLMI, NACCZLMD,
NACCDFT, NACCAGEB.
2) Using loops, replace the missing codes 99 and -99 for the following neuropsychological scores:
NACZZMS, NACCLMI, NACCZLMD and NACCDFT
3) Calculate the mean of the scores: NACZZMS, NACCLMI, NACCZLMD and NACCDFT and name the
new variable “Cognition”
4) Identify the subjects who have mean cognitive score less than -1.5 (Note: SAS thinks that a missing
value is equal to –infinity)
5) Export the matrix of the row numbers of subjects from question 4 in a dataset called IMPAIRED.
6) Within IML, run a regression (proc REG) with the “Cognition” as the dependent variable and age as
the independent variable.

the code which I have used is as follows, I could only solve question 1, I dont know how to approach question 2 and 3 .

libname in "E:\hw";

proc iml;
varnames={"NACCZMMS" "NACCZLMI" "NACCZDFT" "NACCZLMD" "NACCAGEB"};
use in.exercise;
read all var varnames into cg;
print cg[c=varnames];

quit;

8 REPLIES 8

## Re: proc iml

i was doing homework

Please give each of these questions a try and show us the code you have tried. To be honest, we're not going to do homework for you, but we will try to help you if you get stuck.

--
Paige Miller

## Re: proc iml

> Using loops, replace the missing codes 99 and -99 for the following neuropsychological scores:
NACZZMS, NACCLMI, NACCZLMD and NACCDFT

Think about where these variables are in the CG matrix. They are in columns 1-4, right? So you can loop over the rows and columns and use an IF-THEN statement to detect whether any values are 99 or -99. If so, replace them with a SAS missing value.

In developing computer programs, it is often useful to solve a small example before you tackle the larger problem. See if the following example helps you solve your problem. You will need to rewrite the program to use your matrix (CG) instead of the small example matrix:

``````proc iml;
/* replace -99 and 99 with a missing value */
X = {0 -99  1  2,
99   3  4  5,
6   7 99 -99};
do i = 1 to nrow(X);
do j = 1 to 4;
if X[i,j]= 99 | X[i,j]= -99 then
X[i,j] = .;
end;
end;

print X;
``````

## Re: proc iml

hi,

I corrected my code accordingly

libname in "E:\Third sem MPH\BS 803\class 3\hw";

proc iml;
varnames={"NACCZMMS" "NACCZLMI" "NACCZDFT" "NACCZLMD" "NACCAGEB"};
use in.exercise;
read all var varnames into cg;
do i = 1 to nrow(cg);
do j = 1 to 4;
if cg[i,j]= 99 | cg[i,j]= -99 then
cg[i,j] = .;
end;
end;
cognition=cg[:,];
print cognition cg[c=varnames];

quit;

I also took out the means for each col but in the question it has been asked to identify the subjects who have mean score less then -1.5, but here since i have taken out the means of each coloumn , is it impossible to take out to identify the subjects who have mean score less then -1.5

## Re: proc iml

Remark: Since NACCAGEB is not used in this problem, the program will be simpler if you exclude the variable. Then you don't have to subset by columns 1:4.

> 3) Calculate the mean of the scores: NACZZMS, NACCLMI, NACCZLMD and NACCDFT and name the new variable “Cognition”
I think this says to compute the mean across the first four columns for each subject.

``````/* NACZZMS NACCLMI NACCZLMD NACCDFT NACCAGEB */
cg = {0   .  1  2 23,
.   .  .  . 32,
4   3  .  5 25,
1   1  2  1 28};

/* for convenience, extract the first 4 columns */
X = cg[ ,1:4];
cognition = X[ ,:]; /* mean for each subject */
print cognition;
smallCog = (cognition < 1.5 & cognition ^= .); /* 0/1 variable (or use loop) */
print cognition smallCog;
``````

## Re: proc iml

can we find each subject which are less then -1.5 using the loc function

This is what I was thinking

small = loc(X[,1:4]<1.5 ;
print (small) [c=varNames];

## Re: proc iml

Yes, you can use the LOC function to find the row numbers. Add this to the end of my last program:

``````idx = loc(smallCog);
print idx;
``````

## Re: proc iml

oh got that.

and do u have an example where proc iml is used to run linear regression instead of proc reg

## Re: proc iml

The SAS/IML User's Guide discusses linear regression in the Tutorial:

Tutorial: A Module for Linear Regression

From The DO Loop