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

Re: st: a question about -if- command


From   Ulrich Kohler <[email protected]>
To   [email protected]
Subject   Re: st: a question about -if- command
Date   Sat, 18 Jan 2003 23:05:30 +0100

Katsuhide Isa wrote:
> 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.

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. 

regards
uli


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