Home  /  Resources & support  /  FAQs  /  Programming Stata

How do I process parallel lists?

Title   Looping over parallel lists
Author Kevin Crow, StataCorp

You can loop over parallel lists in Stata using the forvalues command and the extended macro function : word #. For example, let us say that you had two lists, cat dog cow pig and meow woof moo oinkoink. In a loop, you would like to have the first iteration be cat and meow, the second iteration be dog and woof, etc. Below is the forvalues command you would use.

. local agrp "cat dog cow pig"

. local bgrp "meow woof moo oinkoink"

. local n : word count `agrp'

. forvalues i = 1/`n' {
  2.    local a : word `i' of `agrp'
  3.    local b : word `i' of `bgrp'
  4.    di "`a' says `b'"
  5. }
cat says meow
dog says woof
cow says moo
pig says oinkoink