# Re: st: if command

 From Fred Wolfe <[email protected]> To [email protected] Subject Re: st: if command Date Tue, 18 Jan 2005 10:59:39 -0600

There are many ways to code efficiently in Stata, but except to tell us that the problem is complex, you don't provide enough information about the problem for us to make suggestions. We need sufficient details.

Fred Wolfe

Fred Wolfe

At 10:13 AM 1/18/2005, 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/
```
Fred Wolfe
National Data Bank for Rheumatic Diseases
Wichita, Kansas
Tel (316) 263-2125     Fax (316) 263-0761
[email protected]

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