Title stata.com
while — Looping
Syntax Description Remarks and examples Also see
Syntax
while exp {
stata commands
}
Braces must be specified with while, and
1. the open brace must appear on the same line as while;
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.
Description
while evaluates exp and, if it is true (nonzero), executes the stata commands enclosed in the
braces. It then repeats the process until exp evaluates to false (zero). whiles may be nested within
whiles. If the exp refers to any variables, their values in the first observation are used unless explicit
subscripts are specified; see [U] 13.7 Explicit subscripting.
Also see [P] foreach and [P] forvalues for alternatives to while.
Remarks and examples stata.com
while may be used interactively, but it is most often used in programs. See [U] 18 Programming
Stata for a description of programs.
The stata commands enclosed in the braces may be executed once, many times, or not at all. For
instance,
program demo
local i = ‘1’
while ‘i’>0 {
display "i is now ‘i’"
local i = ‘i’ - 1
}
display "done"
end
. demo 2
i is now 2
i is now 1
done
. demo 0
done
1