Among various structures in Stata for cycling through lists (whether lists of variable names, numbers, or arbitrary strings) are foreach and forvalues, introduced in Stata 7 in 2001, and for, introduced in Stata 3.1 in 1992, and revised in 5.0 (1997) and 6.0 (1999). Typically, each member of the list supplied is substituted in turn in one or more commands within the structure being used. This is a tutorial specifically designed for Stata users who do little or no Stata programming. Despite being labelled as programming commands, these structures have many uses either interactively or within do files and help impart both speed and system to repetitive tasks. One prerequisite for understanding foreach and forvalues is the idea of a local macro, of which the most difficult part is understanding the strange name. With that idea assimilated, it is relatively easy to see how foreach and forvalues can be used in a large variety of problems. Despite being newer than for in Stata, these structures are recommended over for to new users, or to more experienced users who have made little or no use of any of these to date. For completeness, there is also comparison with for, and some comments on their relative merits.
Download Info
To download:
If you experience problems downloading a file, check if you have the
proper application to
view it first. Information about this may be contained
in the File-Format links below. In case of further problems read
the IDEAS help
page. Note that these files are not on the IDEAS
site. Please be patient as the files may be large.
Did you know? Each page is provided with a technical contact, in case something is not right with the supplied information. See under "publisher info".