---------------- help for xfor ----------------Extended for command ---------------------
Basic syntax
xfor listtype list : stata_cmd_containing_X
The syntax of xfor is identical to for with 2 exceptions described below. For an overview of the for command, see help on for.
The Stata for command is one of its most amazingly powerful, yet incredibly simple commands. The xfor command attempts to extend the range of situations where this command can be used, as described below.
1. Nested "for"s, with nested commands. Consider the "for" command below.
. for A in num 1/2, noh : for B in num 1/2 , noh : display "command 1 a=A b=B" display "command 2 a=A b=B"
We would expect the output of this command to be
command 1 a=1 b=1 command 2 a=1 b=1 command 1 a=1 b=2 command 2 a=1 b=2 command 1 a=2 b=1 command 2 a=2 b=1 command 1 a=2 b=2 command 2 a=2 b=2
Actually, though, the for command creates this output.
command 1 a=1 b=1 command 1 a=1 b=2 command 2 a=1 b=B command 1 a=2 b=1 command 1 a=2 b=2 command 2 a=2 b=B
The xfor command yields the results we expect, see below.
. xfor A in num 1/2, noh : xfor B in num 1/2 , noh : display "command 1 a=A b=B" display "command 2 a=A b=B"
command 1 a=1 b=1 command 2 a=1 b=1 command 1 a=1 b=2 command 2 a=1 b=2 command 1 a=2 b=1 command 2 a=2 b=1 command 1 a=2 b=2 command 2 a=2 b=2
2. Parallel lists
The for command can have problems with parallel lists because the can indicate a separator between lists, or it can be a separator between commands. This is excellently explained in a Stata FAQ at http://www.stata.com/support/faqs/lang/nestfor.html To remove this ambiguity, the xfor command uses a vertical bar | as a separator for the lists. For example, the for command gives an error trying to execute this command.
. for A in num 1/2, noh : for X in num 1/3 Y in num 7/9, noh : di "A,X,Y" > invalid syntax r(198);
This can be executed using the xfor command, by changing for to xfor a > nd by changing the separating lists to a |.
. xfor A in num 1/2, noh : xfor X in num 1/3 | Y in num 7/9, noh : di "A,X,Y > "
1,1,7 1,2,8 1,3,9 2,1,7 2,2,8 2,3,9
Author ------
Michael N. Mitchell Statistical Computing and Consulting UCLA, Office of Academic Computing mnmatucla.edu