Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: loop over variable groups (not individual variables)


From   Tirthankar Chakravarty <tirthankar.chakravarty@gmail.com>
To   statalist@hsphsun2.harvard.edu
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 <mandy.fu1@gmail.com> 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
tchakravarty@ucsd.edu
tirthankar.chakravarty@gmail.com

*
*   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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index