Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: if-then-else

From   Richard Williams <>
Subject   Re: st: if-then-else
Date   Thu, 18 Dec 2003 00:07:15 -0500

At 11:12 PM 12/17/2003 -0500, Christopher F Baum wrote:
On Wed, 17 Dec 2003, Richard Williams wrote:
> In SPSS, I could do something like
> DO IF X1 = 1 and X2 = 3
> Compute Y = 3.
> ELSE IF X3 = 2 and X4 = 17
> Compute Y = 4.
> Compute Y = 5.

Although Richard's point--that most programming languages contain an if-then-else structure--this example shows one of the potential confusions of that structure. What if (x1/x4) = (1,3,2,17) ? In this example, that will yield y=4; the order of the if-else clauses matters. Perhaps this is not very likely to be a realistic application of if-then-else--more likely that the conditions are mutually exclusive--but whether one programs this as if-then-else or, as in Stata, as successive 'replace if' statements, the ordering of these evaluations matters.
No, I don't think so. In SPSS, it will yield Y = 3; because the initial if statement is true none of the following statements will get executed. Order does indeed matter, so you have to make sure that, if multiple conditions are satisfied, the highest priority one comes first, followed by the 2nd highest, etc.

This is admittedly a weird example but similar things do come up where your computation of Y depends on characteristics of the individual. So, for example, if y = admission score, the logic might be something like if applicant is child of an alum and alum is super rich, y = 1; else if applicant has high standardized test scores and high grades y = 2; else y =3. Well hopefully this example isn't realistic either but the idea is that some characteristics automatically get you a 1 no matter how you score on anything else, and given that you didn't get a 1 other characteristics automatically get you a 2 no matter what your values are on anything else lower down the chain, etc. Conditions need not be mutually exclusive; If multiple conditions are met, then the one that came first in the if-then-else construction takes precedence.

Another possible application is where skip patterns cause basically the same or a similar question to be asked at different points in the interview; so, for some people, y = x17, for others y = x33, for everyone else y = x50. The answers to several different questions may determine which other questions you eventually get skipped to.

And for an obtuse solution:

. g c1=3*(x1==1 & x2==3)

. g c2=4*(x3==2 & x4==17)

. g y= cond(max(c1,c2),max(c1,c2),5)

. list x1-x4 y

     | x1   x2   x3   x4   y |
  1. |  1    3    0    0   3 |
  2. |  0    0    2   17   4 |
  3. |  1    2    3    4   5 |
  4. |  1    3    2   17   4 |
  5. |  1    2    3    4   5 |
No, something is wrong there; on case 4, my SPSS code would result in y = 3, not y = 4 like you have it.

Perhaps the moral is to avoid situations like this like the plague, because they are so easy to screw up! But if you must do it, make sure it comes out the way you intended it to.

Richard Williams, Associate Professor
OFFICE: (574)631-6668, (574)631-6463
FAX: (574)288-4373
HOME: (574)289-5227
EMAIL: Richard.A.Williams.5@ND.Edu
WWW (personal):
WWW (department):

* For searches and help try:

© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index