Bookmark and Share

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   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: loop over variable groups (not individual variables)
Date   Fri, 19 Aug 2011 08:18:46 +0100

If this is the real problem, then

reg va
reg va vb vc
reg vd
reg vd ve vf
reg vg
reg vg vh vi

is the simplest and clearest way to do it and the whole apparatus of
defining -local-s and setting up a -foreach- loop gains nothing except
programming practice. If it is not the real problem, the question
still remains of what is simplest and clearest for your real problem.

Nick

On Fri, Aug 19, 2011 at 5:43 AM, Amanda Fu <[email protected]> wrote:
> Hi Tirthankar,
>
> Thank you so much for your helping me out. What you suggested works very well.
>
> Best wishes,
> Amanda
>
> On Thu, Aug 18, 2011 at 11:39 PM, Tirthankar Chakravarty
> <[email protected]> wrote:
>> 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
>>> }
>>>

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index