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 code question


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: loop code question
Date   Sun, 12 Aug 2012 09:11:03 +0100

1.

local list1 "A B C"
local list2 "a b c"
local n1 : word count `list1'
local n2 : word count `list2'
if `n1' != `n2' {
        di as err "different counts"
}

forval i = 1/`n1' {
        local first : word `i' of `list1'
        local second : word `i' of `list2'
        ... `first' `second'

See also

SJ-3-2  pr0009  . . . . . . . . . . . . . Speaking Stata:  Problems with lists
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q2/03   SJ 3(2):185--202                                 (no commands)
        discusses ways of working through lists held in macros


2. No need for new variables

foreach y in 1994 1998 2005 {

              .... if inlist(year, `=`y' - 1', `y', `=`y'+1')
}



On Sat, Aug 11, 2012 at 10:12 PM, Kaur, Dupinderjeet
<dxk110730@utdallas.edu> wrote:
> Dear All,
>
> I have two questions on using a loop to run regressions.
>
> 1)      I want to use pair of variables together when doing foreach, right now to do this I am doing a double loop and delegating combinations that I don't want.
> Here is the code, I need A a, B b, C c combination. How can I do this in a single loop?
>                 foreach x in A B C{
>                                 foreach y in a b c{
>                                 eststo: quietly reg dependent independent  `x' `y'
>                                 }
>                 }
> 2)      I have panel data with ID and year. I want to run event regressions. For example if my data is from 1990 to 2010, and I have identified events in 1994,
> 1998, 2005 then I want to run three regressions capturing 2 years data on both sides of the event year. Can I do a categorical variable that differentiates
> these events and then do a loop for regression, I am not sure how to do this exactly.
>
> I am a second year PhD student and new to Stata, so any guidance on these questions would be greatly appreciated.
>
*
*   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