Dr Nicholas Cox n.j.cox@durham.ac.uk
Assistant Professor
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.
Cox, N. J. (2020). Speaking Stata: Loops, again and again. The Stata Journal, 20(4), https://doi.org/10.1177/1536867x20976340
Journal Article Type | Article |
---|---|
Online Publication Date | Dec 22, 2020 |
Publication Date | 2020 |
Deposit Date | Sep 22, 2021 |
Publicly Available Date | Sep 22, 2021 |
Journal | The Stata Journal: Promoting communications on statistics and Stata |
Print ISSN | 1536-867X |
Electronic ISSN | 1536-8734 |
Publisher | SAGE Publications |
Peer Reviewed | Peer Reviewed |
Volume | 20 |
Issue | 4 |
DOI | https://doi.org/10.1177/1536867x20976340 |
Public URL | https://durham-repository.worktribe.com/output/1235247 |
Published Journal Article
(397 Kb)
PDF
Publisher Licence URL
http://creativecommons.org/licenses/by/4.0/
Copyright Statement
https://creativecommons.org/licenses/by-nc/4.0/This article is distributed under the terms of the Creative Commons Attribution-NonCommercial 4.0 License (https://creativecommons.org/licenses/by-nc/4.0/) 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 (https://us.sagepub.com/en-us/nam/open-access-at-sage).
Stata tip 151: Puzzling out some logical operators
(2023)
Journal Article
Speaking Stata: Automating axis labels: Nice numbers and transformed scales
(2022)
Journal Article
Stata tip 148: Searching for words within strings
(2022)
Journal Article
Speaking Stata: The largest five - A tale of tail values
(2022)
Journal Article
Stata tip 145: Numbering weeks within months
(2022)
Journal Article
About Durham Research Online (DRO)
Administrator e-mail: dro.admin@durham.ac.uk
This application uses the following open-source libraries:
Apache License Version 2.0 (http://www.apache.org/licenses/)
Apache License Version 2.0 (http://www.apache.org/licenses/)
SIL OFL 1.1 (http://scripts.sil.org/OFL)
MIT License (http://opensource.org/licenses/mit-license.html)
CC BY 3.0 ( http://creativecommons.org/licenses/by/3.0/)
Powered by Worktribe © 2025
Advanced Search