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 at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: Double loop over unbalanced panel


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Double loop over unbalanced panel
Date   Fri, 2 Nov 2012 20:39:29 +0000

I think the best advice depends on what -formula()- is, which you
don't tell us.

That said

foreach of numlist ...

in both your examples can be recast more simply as -forval- loops.

Nick

On Fri, Nov 2, 2012 at 8:12 PM, Fernando Luco <flucoestatalist@gmail.com> wrote:
> Hi,
>
> I have an unbalanced panel and I need to do a loop over individuals and
> observations. My data looks as follows (I have other variables that are not
> represented in this example)
>
> id t  chc
> 1 1 .5
> 1 2 .
> 1 3 .
> 1 4 .
> 1 5 .
> 2 3 1
> 2 4 .
> 2 5 .
> 2 6 .
>
> What I want to do is, for each individual (id) to complete the values in
> chc according to a formula that depends on other variables. However, the
> number of times that each individual appears differs across individuals. I
> would like to do something such as
>
> foreach k of numlist 1/2 { /*the 2 has to be changed to the total number of
> individuals*/
> foreach w of numlist 2/T_k { /* where T_k corresponds to the number of
> times individual 2 appears*/
> replace chc=formula(`w')
> }
> }
>
> I don't know how to define T_k in the correct way such that it varies
> across individuals (so 5 for individual 1 and 4 for individual 2).
>
> Thanks,
>
> Fernando
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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