Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: loop over variable groups (not individual variables)
From
Tirthankar Chakravarty <[email protected]>
To
[email protected]
Subject
Re: st: loop over variable groups (not individual variables)
Date
Thu, 18 Aug 2011 20:39:04 -0700
You are almost there:
/*********************************************/
clear*
// generate some data
set obs 100
foreach x of newlist va vb vc vd ve vf vg vh vi {
g `x' = runiform()
}
// create the locals
local v1 "va "
local v2 "va vb vc"
local v3 "vd "
local v4 "vd ve vf"
local v5 "vg "
local v6 "vg vh vi"
// regressions
forv i=1/6 {
reg `v`i''
}
/*********************************************/
T
On Thu, Aug 18, 2011 at 7:13 PM, Amanda Fu <[email protected]> wrote:
> Hi All,
>
> I would like to loop over the following set of variable groups (
> instead of individual variables). But my syntax gives looping over
> individual variables. May I have some suggestion on how to fix my
> command? Thank you for your time!
>
> ************define variable list *********************
> local v1 "va "
> local v2 "va vb vc"
> local v3 "vd "
> local v4 "vd ve vf"
> local v5 "vg "
> local v6 "vg vh vi"
>
> My goal is to loop over v1,v2,...,v6, like this:
> reg va
> reg va vb vc
> reg vd
> reg vd ve vf
> reg vg
> reg vg vh vi
>
> That is, when it comes to v2, I want to indicate "va vb vc" together
> instead of looping over va vb vc one by one. I have tried the
> following 2 ways. But both of them gives loop over each variable,
> instead of looping over local list.
>
> ********1**************
> local i=1
> foreach v in `v`i' {
> reg y `v'
> local i=`i'+1
> }
>
> *********2************
> local i=1
> foreach v in local v`i' {
> reg y `v'
> local i=`i'+1
> }
>
> Best,
> Amanda
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
>
--
Tirthankar Chakravarty
[email protected]
[email protected]
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/