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

Re: st: if command


From   Nick Winter <[email protected]>
To   [email protected]
Subject   Re: st: if command
Date   Tue, 18 Jan 2005 14:20:24 -0500

There is no easy way to get around testing the condition twice. But you could simplify the code a bit like this:

. gen byte big = (fpi == "big") if !missing(fpi)
. gen var1 = 1 if big==1
. gen var2 = 1 if big==1
. gen var1 = 2 if big==0
. gen var2 = 12 if big==0

This doesn't reduce the number of tests of the -if- condition, but it does simplify considerably the coding. Depending on what your concern is, this might be a good solution.

--Nick Winter


At 11:13 AM 1/18/2005 -0500, you wrote:

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/
--------------------------------------------------------
Nicholas Winter 607.255.8819 t
Assistant Professor 607.255.4530 f
Department of Government [email protected] e
308 White Hall falcon.arts.cornell.edu/nw53 w
Cornell University
Ithaca, NY 14853-4601

*
* 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