## Proc IML: USe a matrix as an index to another matrix.

I am trying to create a 3 x13 matrix called it deaths, that contains the sum of the number of deaths for each of three years.

The matrix called outcome is a column vector with each row containing 1 to 3 years of observations for each person. Let's say there are 1,000 rows. The matrix called time also has 1,000 rows, but in addition, each of 3 columns contains an indicator for whether the record is from time 1 (col 1), time 2 (col 2) and time 3 (col 3).   I want to sum the deaths in outcome for each individual year. That is, I want to use the indicator in the time matrix to sum those specific rows in the outcome matrix.

Example:

Outcome is first column below. time (col1) time (col 2) time (col3) are the columns of the time matrix...

row 1 0   1    0  0

row 2 0   0    1  0

row 3 0   0    0  1

row4  0    1   0  0

row 5 1    0   1  0.

So the matrix that I want called deaths would sum row 1 and 4, row 2 and 5, to reflect the one and two years totala,

I use the following code that does not work:

deaths=j(3,1,0);
do yr=1 to 3;

deaths[yr]=sum(outcome[time[yr]=1,]));

end;
run;

Does anyone have a suggestion basically for using a matrix to select specific rows from another matrix.

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions

## Re: Proc IML: USe a matrix as an index to another matrix.

I assume there is a typo in the first sentence and you want a 3x1 matrix.

It sounds like you need to use the LOC function to find the rows for each year. Then (if any rows were found), use those row indices to subset the OUTCOME variable. The links provide more information and examples.

``````proc iml;
outcome = {0,0,0,0,1};
time = {1 0 0,
0 1 0,
0 0 1,
1 0 0,
0 1 0};

deaths=j(3,1,0);
do yr=1 to 3;
idx = loc(time[,yr]=1);    /* find 1s in each column */
if ncol(idx) > 0 then do;  /* were any 1s found? */
print yr idx;
deaths[yr]=sum(outcome[idx]);  /* sum the corresponding rows of the OUTCOME variable */
end;
end;
print deaths;``````
4 REPLIES 4

## Re: Proc IML: USe a matrix as an index to another matrix.

I am guessing a little, but is the missing ingredient the LOC function?   So you would need something like:

``deaths[yr]=sum(outcome[ loc(time[ ,yr]=1) ,]));``

## Re: Proc IML: USe a matrix as an index to another matrix.

yes thank you very concise

## Re: Proc IML: USe a matrix as an index to another matrix.

I assume there is a typo in the first sentence and you want a 3x1 matrix.

It sounds like you need to use the LOC function to find the rows for each year. Then (if any rows were found), use those row indices to subset the OUTCOME variable. The links provide more information and examples.

``````proc iml;
outcome = {0,0,0,0,1};
time = {1 0 0,
0 1 0,
0 0 1,
1 0 0,
0 1 0};

deaths=j(3,1,0);
do yr=1 to 3;
idx = loc(time[,yr]=1);    /* find 1s in each column */
if ncol(idx) > 0 then do;  /* were any 1s found? */
print yr idx;
deaths[yr]=sum(outcome[idx]);  /* sum the corresponding rows of the OUTCOME variable */
end;
end;
print deaths;``````

## Re: Proc IML: USe a matrix as an index to another matrix.

Thank you so much. This is a nice concise section of code.

From The DO Loop