New Contributor
Posts: 3

# referencing matrices

I am in great need of help with this:

this is the matrix that I wish to fill

CI=

samplelowerupper
1
2

the contents I need to fill it with are in a matrix like this:

ESTS=

sampleestimate

1

.3
1.2
1.4
2.01
2.9
2.75

the next matrix tells me which rows I need to pull from ESTS into the matrix at the very top (CI):

LIMS=

samplelim1lim2
113
212

what I wish to end up with is this:

CI=

samplelowerupper
1.3.4
2.01.9

I can not for the life of me figure this out.

I'll appreciate any help I get on this.

SAS Super FREQ
Posts: 4,165

## Re: referencing matrices

Use the UNIQU-LOC trick to extract the i_th sample. Within each sample, use the LIMS matrix to determine the rows and use indexing to pull out the correct numbers.

New Contributor
Posts: 3

## Re: referencing matrices

Rick,

Thanks so much for responding.  Actually I already learned the unique_by statement trick from one of your earlier posts and it is amazing!  Believe it or not, it's the actual indexing statement that is tripping me up.  I already have the ESTS matrix split into submatrices by sample no.  I just can't get the correct observations pulled from it.  I'll show you some of the code:

read all var{sample lo hi} into worklim;

use thirdsorted;

read all var{sample mc3estimates} into workests;

unique_rows400=uniqueby(workests,1,1:nrow(workests));

do k=1 to nrow(unique_rows400);

if k=nrow(unique_rows400) then index=unique_rows400:nrow(workests);

else index=unique_rows400:unique_rows400[k+1]-1;

submat400=workests[index,];

print submat400;

end;

I am struggling with something like------

confint=j(nrow(worklim),2,0);

confint[i,1:2]=workests[worklim,worklim[i,2]];

Rachel

SAS Super FREQ
Posts: 4,165

## Re: referencing matrices

You almost got it. See if the following helps.  I wrote out each step on a separate line in case you want to insert PRINT statements inside the DO loop to see how it works.

proc iml;
worklim = {
1 1 3 ,
2 1 2
};

workests = {
1 .3 ,
1 .2 ,
1 .4 ,
2 .01 ,
2 .9 ,
2 .75
};

confint=j(nrow(worklim),2,0);
unique_rows=uniqueby(workests,1);
unique_rows = unique_rows // (nrow(workests)+1); /* append final row */
do k=1 to nrow(unique_rows);
index=unique_rows:unique_rows[k+1]-1;   /* rows of workests */
submat=workests[index,2];                  /* extract the values */
rows = worklim[k,2:3];                     /* rows of submat */
confint[k,]=rowvec( submat[rows] );
end;
print confint;

New Contributor
Posts: 3

## Re: referencing matrices

Rick,

Thank you from the ends of the earth and back!!!  That worked like a charm.  I put in print statements like you said so I could see the "magic" happen.  I did not realize that you have to append the last row in the unique_by matrix......I tried taking it off just for fun and it doesn't fill the last row.  So, I'm guessing it is there to make the do loop complete?  Thanks again.

Rachel

SAS Super FREQ
Posts: 4,165

## Re: referencing matrices

You don't have to append the last row. It is a trick that I use to avoid having an IF-THEN/ELSE statement inside the DO loop (as in your original code).  It makes the code a little easier to read, and slightly more efficient, as explained in this blog post: Avoid unnecessary IF-THEN statements in loops - The DO Loop

Discussion stats
• 5 replies
• 469 views
• 0 likes
• 2 in conversation