From | Richard Williams <[email protected]> |
To | [email protected] |
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: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.
> 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.
> ELSE
> Compute Y = 5.
> END IF.
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, something is wrong there; on case 4, my SPSS code would result in y = 3, not y = 4 like you have it.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 | +-----------------------+
© Copyright 1996–2024 StataCorp LLC | Terms of use | Privacy | Contact us | What's new | Site index |