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

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
Rick_SAS
SAS Super FREQ

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;

View solution in original post

4 REPLIES 4
IanWakeling
Barite | Level 11

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) ,]));
Sanscrit_2prov
Obsidian | Level 7

yes thank you very concise

Rick_SAS
SAS Super FREQ

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;
Sanscrit_2prov
Obsidian | Level 7

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

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 4 replies
  • 1893 views
  • 3 likes
  • 3 in conversation