Stata 15 help for ifcmd

[P] if -- if programming command


if exp { or if exp single_command multiple_commands }

which, in either case, may be followed by

else { or else single_command multiple_commands }

If you put braces following the if or else,

1. the open brace must appear on the same line as the if or else;

2. nothing may follow the open brace except, of course, comments; the first command to be executed must appear on a new line;

3. the close brace must appear on a line by itself.


The if command (not to be confused with the if qualifier; see if) evaluates exp. If the result is true (nonzero), the commands inside the braces are executed. If the result is false (zero), those statements are ignored, and the statement (or statements if enclosed in braces) following the else is executed.

See exp for an explanation of expressions.


Remarks are presented under the following headings:

Typical use: Example 1 Typical use: Example 2 Typical use: Example 3 Avoid single-line if and else with ++ and -- macro expansion

Typical use: Example 1

program ... syntax varlist [, Report ] ... if "`report'" != "" { (logic for doing the optional report) } ... end

Typical use: Example 2

program ... syntax varlist [, Adjust(string) ] ... if "`adjust'" != "" { if "`adjust'" == "means" { ... } else if "`adjust'" == "medians" { ... } else { display as err /* */ "specify adjust(means) or adjust(medians)" exit 198 } } ... end

Typical use: Example 3

program ... syntax ... [, ... n(integer 1) ... ] ... if `n'==1 { local word "one" } else if `n'==2 { local word "two" } else if `n'==3 { local word "three" } else { local word "big" } ... end

Avoid single-line if and else with ++ and -- macro expansion

Do not use the single-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' }

Technical note

What was just said also applies to macro-induced execution of class programs that have side effects. Consider

if (...) somecommand `.clspgm.getnext'

Class member program .getnext would execute regardless of whether the condition were true or false. In this case, code

if (...) { somecommand `.clspgm.getnext' }

Understand that the problem only arises when macro substitution causes the invocation of the class program. There would be nothing wrong with coding

if (...) `.clspgm.getnext'

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