I am in great need of help with this:
this is the matrix that I wish to fill
CI=
sample | lower | upper |
---|---|---|
1 | ||
2 |
the contents I need to fill it with are in a matrix like this:
ESTS=
sample | estimate |
---|---|
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=
sample | lim1 | lim2 |
---|---|---|
1 | 1 | 3 |
2 | 1 | 2 |
what I wish to end up with is this:
CI=
sample | lower | upper |
---|---|---|
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.
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.
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
else index=unique_rows400
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]];
Thanks for your help Rick!
Rachel
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
submat=workests[index,2]; /* extract the values */
rows = worklim[k,2:3]; /* rows of submat */
confint[k,]=rowvec( submat[rows] );
end;
print confint;
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
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
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.