Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down on April 23, and its replacement, **statalist.org** is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
Nick Cox <njcoxstata@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: RE: Problems with execution of conditional branching.. |

Date |
Thu, 14 Mar 2013 11:58:48 +0000 |

A footnote to Sarah's very clear explanation: The help does warn that the -if- qualifier and the -if- command are different. See also FAQ . . . . . . . . . . . . . . . . . . . . if command versus if qualifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . J. Wernow 4/05 I have an if or while command in my program that only seems to evaluate the first observation. What's going on? http://www.stata.com/support/faqs/programming/ if-command-versus-if-qualifier/ That said, the FAQ has it more or less backwards in terms of what people ask. The frequently asked question is much more usually something like "I don't understand what the -if- command is doing, as it is not giving the results I expect" and the answer is then often "the -if- command is evaluating your first observation only -- which is unlikely to be what you want -- and so you need the -if- qualifier instead". Regardless, this difference is so often misunderstood that the help could usefully be strengthened on this point, if only by referring explicitly to the FAQ. My understanding is that SAS differs on this point and my experience is that SAS users coming to Stata are often bitten by the difference. Nick On Thu, Mar 14, 2013 at 1:22 AM, Sarah Edgington <sedging@ucla.edu> wrote: > The problem is that the if statements here are only evaluating your first > observation. See -help ifcmd- and -help if- for more details. > The thing that I think isn't entirely clear from the helpfile is that the > programming if checks the conditional statement once and if it's true it > takes the action in the braces. Unless otherwise specified it evaluates the > first observation in the data when the conditional involves a variable. > To get an observation by observation evaluation of a conditional you have to > use the if expression after a command. > So -gen event_month=substr(opendate,6,1) if comma_3==2 & comma_2==1- will > check that condition for each observation and create the relevant variable. > > Contrast that with a simplification of your first conditional (written as a > single line instead of in braces) > -if comma_3==2 & comma_2==1 gen event_month = substr(opendate, 6,1)- > Here Stata is only evaluating the conditional once. Since it involves > variables (which, by virtue of being variables are going to be different > across observations) there has to be some rule about what observation is > used to evaluate this statement. Since you haven't specified an observation > number to evaluate, it defaults to the first observation. > > So running your code will perform only one of the four possible sets of > commands you specified. Which one it performs depends entirely on the first > observation in the data. * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/

**References**:**st: Problems with execution of conditional branching..***From:*Mani Subramani <subra010@umn.edu>

**st: RE: Problems with execution of conditional branching..***From:*"Sarah Edgington" <sedging@ucla.edu>

- Prev by Date:
**RE: st: -oaxaca- by Ben Jann** - Next by Date:
**Re: st: Prediction at the 'mean' of a categorical- xi3?** - Previous by thread:
**Closure on Statalist [was: Re: st: RE: Problems with execution of conditional branching..]** - Next by thread:
**st: itrim a long local string in extended_fcn** - Index(es):