BookmarkSubscribeRSS Feed
jcub
Calcite | Level 5

I have the following group means vector that I got from IML:

0.3906780.36970170.34482390.32645630.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?

6 REPLIES 6
PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
jcub
Calcite | Level 5

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.

PaigeMiller
Diamond | Level 26

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.

 

 

--
Paige Miller
jcub
Calcite | Level 5
As I said, that is not the purpose of this.
PaigeMiller
Diamond | Level 26

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

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.

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 714 views
  • 0 likes
  • 3 in conversation