Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Stata, how can I append to a local varlist during a loop?

Tags:

list

stata

I am trying to run multiple regressions where, on each iteration, another independent variable is added to the regression on each loop?

local vlist0 foo bar dar
local vlist1

foreach item in `vlist0'
    [add `item' to `vlist1']
    regress dependentVar `vlist1'

I can't seem to find any documentation on appending to local varlists or anything relating to this, so your help is greatly appreciated.

Thanks!

like image 712
Drew Avatar asked Apr 02 '15 13:04

Drew


People also ask

What does foreach do in stata?

foreach repeatedly sets local macro lname to each element of the list and executes the commands enclosed in braces. The loop is executed zero or more times; it is executed zero times if the list is null or empty.


1 Answers

Some technique:

local vlist0 foo bar dar

local vlist1
foreach item of local vlist0 {
    local vlist1 `vlist1' `item'
    display "`vlist1'"
}

This appends the contents of the local, along with the new item, to the local itself.

Notice what this really does: redefine local vlist1 each time around the loop. The new definition is the previous definition, plus the new item. The first time around the loop vlist1 is empty, but that is not illegal and is well-behaved.

like image 79
Roberto Ferrer Avatar answered Sep 23 '22 19:09

Roberto Ferrer