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

Re: st: a question about -if- command


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   Re: st: a question about -if- command
Date   Sun, 19 Jan 2003 16:54:43 -0000

Katsuhide Isa

> I wrote the following two simple statements in the same
> do file. But when executed, the latter ('NG') returned
> nothing, while the former ('OK') returned the desired
> results:
>
> ----------
> /*OK*/
> l  bonus if age == 58 & year ==2001
> pause
> l  bonus if age == 59 & year ==2001
> pause
>
> /*NG*/
> if age == 58 & year ==2001{
>  l  bonus
>  pause
> }
> else if age == 59 & year ==2001{
>  l  bonus
>  pause
> }
> ----------
> This may be a preliminary question, but I don't
> understand why.this difference occurs.
> If there is any syntax error in the latter statements,
> Stata should stop and gives an error message. But
> they are executed without stopping, which makes me
> all the more perplexed.

Ulrich Kohler

The first statement uses the "if"-qualifier. As -help if- states,
there is another -if- used in Stata programming, which you use in your
second
statement. This programming command evaluates an expression. But
unlike the
"if" qualifier the if-command does not evaluate the expression for
each
observation of the dataset. When used with a variable the if-command
only
evaluates the expression for the first observation. Your code:

if age == 58 & year ==2001{
	l  bonus
	pause
}

therefore means: if the first observation in your dataset is 58 years
old and
from year 2001, Stata is going to list _all_ observations.

You should not use the if programing command for an expression
containing a
variable. The if programing command should be used for expression
containing
macros.

>>> In addition, see the FAQ

I have an if command in my program that only
seems to evaluate the first observation. What's going on?
http://www.stata.com/support/faqs/lang/ifqualifier.html

Nick
[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/



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