Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 11-25-2019 09:29 AM
(1810 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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) ,]));`

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

yes thank you very concise

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

**SAS Innovate 2025** is scheduled for May 6-9 in Orlando, FL. Sign up to be **first to learn** about the agenda and registration!

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.