Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: single-line vs multiple-line -if- command


From   Tak-wai Chau <[email protected]>
To   [email protected]
Subject   Re: st: single-line vs multiple-line -if- command
Date   Fri, 06 Apr 2007 12:53:41 -0400

Hi,

Actually the explanation is in the help file:

http://www.stata.com/help.cgi?ifcmd

" Do not use the singe-line forms of if and else--do not omit the braces--when the action includes the `++' or `--' macro-expansion operators. For instance, do not code

if (...) somecommand `++i'

Code instead,

if (...) { somecommand `++i' }

In the first example, i will be incremented regardless of whether the condition is true or false because macro expansion occurs before the line is interpreted. In the second example, if the condition is false, the line inside the braces will not be macro expanded and so i will not be incremented.

The same applies to the else statement; do not code

else somecommand `++i'

Code instead,

else { somecommand `++i' } "

Tak Wai

Zurab Sajaia wrote:

Dear all,

I encountered a strange behavior -if- and/or macro inline incrementation operators and was wandering what's going on.

Lets consider this example:

local a=1
local b=1

if 0 local a1=`a++'

if 0 {
local b1=`b++'
}

display `a', `b'
2 1

Although both expressions are false an therefore neither a1 nor b1 will be created, `a' is incremented anyways. If we say that two versions of -if- statement are equivalent then I'd expect exactly same outcome no matter what type of commands and operations (like inline macro substitution) is used. Am I confusing something here?

I would appreciate any thoughts about the issue,

Thanks,
Zurab
*
* 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/

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