1

In Python I can easily extract pairs out of lists:

>>>list1 = [1, 2, 3]
>>>list2 = [4, 5, 6]
>>>zip(list1, list2)
[(1, 4), (2, 5), (3, 6)]

How can I achieve the same result in Stata? If I have two locals, both containing the same number of elements, how can produce a Python-like "zip"? (Googling this is a nightmare because of the zero-inflated Poisson - i.e., ZIP - model...)

Parzival
  • 2,004
  • 4
  • 33
  • 47

2 Answers2

1

I don't think there's a dedicated command/function for that, but consider parallel lists:

local agrp "cat dog cow pig"

local bgrp "meow woof moo oinkoink"

local n : word count `agrp'

forvalues i = 1/`n' {
      local a : word `i' of `agrp'
      local b : word `i' of `bgrp'
      di "`a' says `b'"
}

See http://www.stata.com/support/faqs/programming/looping-over-parallel-lists/.

Roberto Ferrer
  • 11,024
  • 1
  • 21
  • 23
1

You can combine two vectors as a matrix in Stata and in Mata (where the vectors can contain strings and indeed much else). Here's Stata:

. mat list1 = [1, 2, 3]'
. mat list2 = [4, 5, 6]'
. mat list = list1, list2 
. mat li list 

   list[3,2]
    c1  c1
r1   1   4
r2   2   5
r3   3   6

What you would want to do with such a structure in Stata? As @Roberto Ferrer's answer indicates, you would often just loop in parallel over the separate lists.

Nick Cox
  • 35,529
  • 6
  • 31
  • 47
  • I was trying to loop through pairs of strings. – Parzival Oct 21 '14 at 17:51
  • 1
    Might be easier in Mata, or just `foreach` in Stata. My paper at http://www.stata-journal.com/sjpdf.html?articlenum=pr0009 covers some possibilities. See Section 3. – Nick Cox Oct 21 '14 at 18:15