Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 36 ยท Programming and Do-files

Loops: foreach, forvalues, while and levelsof

Automate repetitive Stata work with foreach, forvalues and while, loop over variables, numbers, files and categories, and use if-else and continue. Python stand-ins.

If you find yourself copying a command and changing one word, you need a loop. Stata has three loop commands: foreach for lists, forvalues for number ranges and while for conditions. Combined with macros they let you clean fifty variables, fit a model for every country, or read every file in a folder with a few lines.

Stata cannot run here, so Stata code is read-only. The runnable blocks are Python stand-ins that print the commands a Stata loop would execute, which is a good way to understand exactly what the loop does.

foreach over a list

STATA
foreach v in wage hours tenure {
    summarize `v'
    generate ln_`v' = ln(`v')
}

On each pass the local macro v takes the next item. Inside the braces you use `v'. Rules that trip beginners up: the opening brace must be the last thing on the foreach line, and the closing brace must be on its own line.

The list can come from different sources:

STATA
foreach v of varlist wage-tenure {          // a range of variables in the data
    replace `v' = . if `v' < 0
}
foreach v of varlist _all { }                // every variable
foreach v of local controls { }              // items in a local macro
foreach n of numlist 1 3 5 7 { }             // numbers
foreach f in "a.csv" "b.csv" { }

of varlist checks that the names exist and expands wildcards (inc*) and ranges, which in does not. Use it for variable names. For type-specific loops, build the list first: ds, has(type string) followed by foreach v of varlist r(varlist)'`.

forvalues over numbers

STATA
forvalues year = 2015/2024 {
    display "Processing `year'"
}
forvalues i = 10(5)50 {        // 10, 15, 20, ..., 50
    display `i'
}

Use forvalues instead of foreach ... of numlist for plain counters; it is faster and clearer. A common use is building variables or models for each period:

STATA
forvalues y = 2018/2022 {
    use "data/raw/survey_`y'.dta", clear
    generate year = `y'
    tempfile f`y'
    save `f`y''
}

while loops and conditions

STATA
local i = 1
while `i' <= 5 {
    display `i'^2
    local i = `i' + 1
}

while repeats until the condition is false; always make sure it changes. Inside any loop you can use if/else (a different thing from the if qualifier on a command), continue to skip to the next pass and continue, break to leave the loop:

STATA
foreach v of varlist * {
    capture confirm numeric variable `v'
    if _rc continue                // not numeric: skip it
    quietly summarize `v'
    if r(min) < 0 display "`v' has negative values"
}

_rc is the return code of the most recent capture; zero means success.

Looping over categories with levelsof

levelsof stores the distinct values of a variable in a macro:

STATA
levelsof region, local(regions)
foreach r of local regions {
    display "Region `r'"
    regress wage educ exper if region == `r'
    estimates store reg`r'
}

For string categories, add clean and quote the loop value, or loop over the value labels. Stata also has bysort region: regress wage educ exper, which does the same without a loop, but bysort only works with commands that allow by:. A loop gives you control over what is saved after each pass.

Stand-in: showing the expanded commands

This Python block imitates foreach and forvalues by printing the commands Stata would run. Change the lists and the template to see the expansion.

Python foreach and forvalues expansion (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Nested loops and tables of results

Loops can be nested, and results can be collected into a dataset with postfile, or into a matrix:

STATA
tempname P
postfile `P' str20 variable b se using "results.dta", replace
foreach v of varlist educ exper tenure {
    quietly regress lnwage `v'
    post `P' ("`v'") (_b[`v']) (_se[`v'])
}
postclose `P'
use "results.dta", clear
list

postfile is the standard way to store results from simulations too, for example a Monte Carlo loop with forvalues rep = 1/500. The shorter simulate and bootstrap commands wrap this pattern for you.

Python Collecting loop results (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Good practice

  • Develop with one pass first (display the commands), then switch the loop on.
  • Wrap risky steps in capture and report failures instead of silently continuing.
  • Use quietly or noisily blocks to control output, and set trace on to debug.
  • Prefer built-in vectorised commands (egen, by:, reshape) when they exist; they are faster than loops over observations.
  • Avoid looping over individual observations (one pass per row); it is very slow on large data. Use egen, by: or generate instead.

Programs let you package a loop body for reuse: continue to writing programs.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which loop is best for years 2015 to 2024?

    forvalues iterates over a numeric range and is the clearest choice for counters.
  2. 2. What does foreach v of varlist wage-tenure iterate over?

    of varlist expands ranges and wildcards against the variables in memory.
  3. 3. How do you get the distinct values of region into a macro?

    levelsof stores the sorted distinct values in the named local.
  4. 4. What does continue do inside a loop?

    continue skips to the next iteration; continue, break exits the loop.

Summary

  • foreach loops over lists, variable names, locals and numlists; forvalues over ranges; while over a condition.
  • levelsof turns a categorical variable into a loop list; capture, _rc and continue handle exceptions.
  • postfile collects results from loops into a dataset.
  • The Python blocks are stand-ins that print the expansion.

Next: Writing programs and ado-files