BookmarkSubscribeRSS Feed
lvm
Rhodochrosite | Level 12 lvm
Rhodochrosite | Level 12

I wanted to tell you more about LSMESTIMATE, a really cool statement (thank you SAS developers for adding this a few years ago!). Check out this article:

http://support.sas.com/resources/papers/proceedings11/351-2011.pdf

I have also put a demonstration below, generating two factors, a with 3 levels and b with 4 levels. Only a residual variance, but that doesn't matter for the demonstration. I want to compare specific pairs of interaction means. The LSMESTIMATE statement works on the means, not on the underlying factor effect parameters (well, it actually works on the latter, but the user refers to the means, which is very useful with factorials). Here is a very important point: you must know the order that the procedure is storing the means. This depends on the order of terms in CLASS and in MODEL statements. It is very helpful to have the S option on the model statement, to see the order. Here you can see that the order is:

a     b

1     1

1     2

1     3

1     4

2     1

2     2

...etc. ...

You can also see this with the LSMEANS a*b output. The program shows how to get a2b2 vs. a2b3, and also a2b2 vs a3b2. I show how to do this with the positional syntax (knowing the order exactly); the trailing zeros are not needed, but it helps to show that there are 12 possible means. Then I show the new nonpositional syntax, where you only give the coefficients for means of interest. The article describes all of this, and much more. The diff option in LSMEANS will confirm that I am getting the contrasts of interest. By the way, you can also do this with the older ESTIMATE or CONTRAST statement, but you have to keep track of the solution of fixed effect, a much more tedious thing to do. By the way, you can also do more complex contrasts, such as the average of two means versus another single mean.

data test;

do a = 1 to 3;

do b = 1 to 4;

do rep = 1 to 5;

y = 10 + 2*(a-1) + 1*(b-1) + 2*(a-1)*(b-1) + rannor(1);

output;

end;

end;

end;

run;

proc glimmix data=test;

class a b;

model y = a b a*b / s;

lsmeans a*b / diff;

*use positional syntax to compare some specific means;

lsmestimate a*b 'a2b2 vs a2b3' 0 0 0 0 0 1 -1 0 0 0 0 0;

lsmestimate a*b 'a2b2 vs a3b2' 0 0 0 0 0 1  0 0 0 -1 0 0;

*now do the nonpositional syntax (very cool);

lsmestimate a*b 'np a2b2 vs a2b3' [1, 2 2] [-1, 2 3];

lsmestimate a*b 'np a2b2 vs a3b2' [1, 2 2] [-1, 3 2];

run;

SAS Innovate 2025: Call for Content

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!

Submit your idea!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 15 replies
  • 4168 views
  • 0 likes
  • 3 in conversation