We use cookies to ensure that we give you the best experience on our website. By continuing to browse this repository, you give consent for essential cookies to be used. You can read more about our Privacy and Cookie Policy.

Durham Research Online
You are in:

Speaking Stata: Loops, again and again

Cox, Nicholas J. (2020) 'Speaking Stata: Loops, again and again.', The Stata Journal: Promoting communications on statistics and Stata, 20 (4).


Two commands in official Stata, foreach and forvalues, provide structures for looping through lists of values (variable names, numbers, arbitrary text) and repeating commands using members of those lists in turn. These commands may be used interactively, and none is restricted to use in Stata programs. They are explained and compared in some detail with a variety of examples. In addition, a self-contained exposition is given on local macros, understanding of which is needed for use of foreach and forvalues. This column is a revision of the column “How to face lists with fortitude”, which appeared in Stata Journal 2: 202–222 (2002). (The bizarre bibliographical details are too, too extraordinary not to be flagged but were pure happenstance.) The presentation here has been trimmed of now historic content and corrected, improved, and updated in several minor details.

Item Type:Article
Full text:(VoR) Version of Record
Available under License - Creative Commons Attribution 4.0.
Download PDF
Publisher Web site:
Publisher statement: article is distributed under the terms of the Creative Commons Attribution-NonCommercial 4.0 License ( which permits non-commercial use, reproduction and distribution of the work without further permission provided the original work is attributed as specified on the SAGE and Open Access pages (
Date accepted:No date available
Date deposited:22 September 2021
Date of first online publication:22 December 2020
Date first made open access:22 September 2021

Save or Share this output

Look up in GoogleScholar