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

st: if command


From   Cameron Hooper <[email protected]>
To   [email protected]
Subject   st: if command
Date   Tue, 18 Jan 2005 11:13:24 -0500

Hello

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/




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