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

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

Posted 06-05-2021 10:57 AM
(950 views)

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?

6 REPLIES 6

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

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

Paige Miller

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

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.

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

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

Paige Miller

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

As I said, that is not the purpose of this.

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

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

Paige Miller

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

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.

Are you ready for the spotlight? We're accepting content ideas for **SAS Innovate 2025** to be held May 6-9 in Orlando, FL. The call is **open **until September 25. Read more here about **why** you should contribute and **what is in it** for you!

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.