[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

From |
"Nick Cox" <[email protected]> |

To |
<[email protected]> |

Subject |
st: RE: if command |

Date |
Tue, 18 Jan 2005 17:01:48 -0000 |

-if- as a keyword is overloaded. The two ways of using -if- you exemplify are not equivalent -- unless you have single observation datasets. This is explained in an FAQ at http://www.stata.com/support/faqs/lang/ifqualifier.html and elsewhere. I don't know about if ... { } "not work[ing] as expected": that hinges on weighting preconceptions from some other languages more than what is documented for Stata. There are many languages that are like Stata in this respect; or conversely, Stata here imitates some of them. You can collapse your examples in this way gen var1 = (fpi == "big") + 2 * (fpi == "small") or in this way gen var1 = cond(fpi == "big", 1, cond(fpi == "small", 2, .)) or if "big" and "small" were the only possibilities gen var1 = cond(fpi == "big", 1, 2) Having said that, and voting for efficiency rather than inefficiency any day, there's still a lot to be said for code so transparent that it is instantly comprehensible when you revisit. Nested -cond()-s particularly divide the world. It is no gain to bundle a mass of complicated logic into a single statement with many -cond()- examples if you make some subtle logic error. Nick [email protected] Cameron Hooper > What is the best way to achieve the following in stata? I have a > reasonably complex logical test and number of new variables > to generate > as a result of applying the test. Here is a minimal example. The real > problem is more complex, but this captures the spirit: > > gen var1 = 1 if fpi == "big" > gen var2 = 1 if fpi == "big" > replace var1 = 2 if fpi == "small" > replace var2 == 12 if fpi =="small" > > Repeating the condition seems inefficient especially if it was very > complex (it is) and if I have to create many new variables (I > do). I was > thinking some like this: > > if fpi == "big" { > gen var1 = 1 > gen var2 = 1 > } > if fpi == "small { > replace var1 = 2 > replace var2 = 12 > } > > Not much of a saving in this example, but in a more complex > problem the > saving would, I think, be measurable. However, I know from > experimentation and from a previous thread that this code > does not work > as expected. Is there a solution or should I should I just > use the first > coding style? Thanks in advance. > > Cameron > * > * For searches and help try: > * http://www.stata.com/support/faqs/res/findit.html > * http://www.stata.com/support/statalist/faq > * http://www.ats.ucla.edu/stat/stata/ > * * For searches and help try: * http://www.stata.com/support/faqs/res/findit.html * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

**Follow-Ups**:**Re: st: RE: if command***From:*Cameron Hooper <[email protected]>

- Prev by Date:
**RE: st: if command** - Next by Date:
**st: kdens2 and scat3 problems. More general graph problems.** - Previous by thread:
**st: clogit two question addendum-code** - Next by thread:
**Re: st: RE: if command** - Index(es):

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