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: Double loop over unbalanced panel


From   WEBER Sylvain <[email protected]>
To   "<[email protected]>" <[email protected]>
Subject   Re: st: Double loop over unbalanced panel
Date   Sat, 3 Nov 2012 05:53:52 +0000

Hi. 
I think you don't need any loop. - by - can be used instead. 
Try 
bysort id (t): replace chc=(chc[_n-1]*ph+cont)/p if mi(chc)

Note that the - if mi(chc) - condition is very important. Otherwise, you would replace all values of chc; not only missing values. 

Best,

Sylvain

Le 2 nov. 2012 à 14:00, "Fernando Luco" <[email protected]> a écrit :

> Hi Nick,
> 
> The data and the formula look as follows
> 
> input id t str1 ch p str1 chh ph cont con_c chc
> 1 1 "a" 2 "" . 1 0.5 1 1
> 1 2 "b" 3 "a" 4 3 1 .
> 1 2 "b" 3 "a" 4 3 1 .
> 1 3 "a" 1 "b" 2 2 2 .
> 1 4 "c" 4 "a" 3 3 .75 .
> 1 5 "d" 2 "c" 5 1 .5 .
> 2 3 "d" 2 "" . 1 1 2
> 2 4 "a" 3 "d" 3 3 1 .
> 2 5 "b" 1 "a" 2 2 2 .
> 2 6 "a" 2 "b" 2 1 .5 .
> end
> 
> foreach k of numlist 1/2{
> foreach w of numlist 2/T_k{
> replace chc=(chc[`w'-1]*ph+cont)/p if _n==`w' & id==`k'
> }
> }
> 
> Thanks,
> 
> Fernando
> 
> On Fri, Nov 2, 2012 at 3:39 PM, Nick Cox <[email protected]> wrote:
>> 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 <[email protected]> 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/
> *
> *   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index