Yan Song
Yan Song

Reputation: 110

How to substitute two variables in one loop in Stata

I want to be able to carry out the following idea in Stata. I have a bunch of paired names. For instance Ryan and King is a pair. In a pseudo code

keep  if product_name == "i" | product_name == "j"

where Ryan should substitute the i and King sub the j. I want to have a loop that can do this for multiple pairs of the names.

Upvotes: 0

Views: 145

Answers (1)

Roberto Ferrer
Roberto Ferrer

Reputation: 11102

One option is to use parallel lists. Some technique:

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'
      display "`a' says `b'"
}

Substitute the display line with whatever you want.

This is a Stata FAQ.

Upvotes: 4

Related Questions