I have the following group means vector that I got from IML:
0.390678 | 0.3697017 | 0.3448239 | 0.3264563 | 0.2937608 |
I need to construct a nested do loop resulting in a row vector of dimension 1x10 that contains pairwise differences between each of the means here. However, I don't really understand how to do this, as I am new to constructing do loops. So far, what I have is the following, which doesn't work at all and provides different values from what I get when comparing it to proc GLM:
a=5;
do i=1 to a;
do j=(i+1) to a;
DIFF=group_mean[i]-group_mean[j];
end;
end;
Can anyone provide any help on how to do this?
What about this doesn't work?
Are there errors in the log? (If there are errors, show us the ENTIRE log for your PROC IML, do not chop out parts, we need to see the ENTIRE log)
Why do you do this in IML, when SAS has already programmed this in many other PROCs?
There are no errors that arise. However, the values that arise are incorrect. It also returns separate scalars for DIFF and I need to create a 1x10 vector of the differences. Without the DIFF values being differentiated, I can't even concatenate. I'm doing this as part of an independent study to learn how to use IML and better understand the math going on in the background, which is why I'm not using Proc GLM or something comparable. I just tried editing the code to this:
DIM=10;
a=5;
do i=1 to a;
do j=i+1 to a;
do k=1 to DIM;
group_mean[i]=group_mean[1,i];
group_mean[j]=group_mean[1,j];
DIFF=j(1,DIM,.);
DIFF[1,k]=group_mean[i]-group_mean[j];
end;
end;
end;
This provides the following vector, which obviously doesn't work:
. | . | . | . | . | . | . | . | . | 0.0327045 |
But the one value that has been populated matches one of the values from Proc GLM.
PROC GLM already provides you with the ability to compute pairwise mean differences. The LSMEANS statement, with the PDIFF option, gets the job done, and then you don't have to program it yourself.
I have made small changes in your program. In particular, I am moving the line where you define matrix DIFF to outside the loops. If you leave it inside the loops, every time you go through a loop, DIFF is created with missing values everywhere.
DIM=10;
a=5;
count=0;
DIFF=j(1,DIM,.);
do i=1 to a;
do j=i+1 to a;
count=count+1;
group_mean[i]=group_mean[1,i];
group_mean[j]=group_mean[1,j];
DIFF[1,count]=group_mean[i]-group_mean[j];
end;
end;
To generalize and simplify Paige's response, you could also do this:
proc iml;
group_mean = {0.390678 0.3697017 0.3448239 0.3264563 0.2937608};
a=ncol(group_mean);
DIFF=j(1,a*(a-1)/2,.);
count=0;
do i=1 to a;
do j=i+1 to a;
count=count+1;
DIFF[count]=group_mean[i]-group_mean[j];
end;
end;
print DIFF;
There are many ways to compute pairwise differences of elements in a vector. For a discussion, see the article "Computing pairwise differences," which shows vectorized computations that use matrices.
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!
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.