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]

From |
Fernando Luco <flucoestatalist@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: Double loop over unbalanced panel |

Date |
Fri, 2 Nov 2012 16:00:10 -0500 |

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 <njcoxstata@gmail.com> 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 <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/ * * 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/

**Follow-Ups**:**Re: st: Double loop over unbalanced panel***From:*WEBER Sylvain <sylvain.weber@unine.ch>

**References**:**st: Double loop over unbalanced panel***From:*Fernando Luco <flucoestatalist@gmail.com>

**Re: st: Double loop over unbalanced panel***From:*Nick Cox <njcoxstata@gmail.com>

- Prev by Date:
**Re: st: svyset** - Next by Date:
**Re: st: svyset** - Previous by thread:
**Re: st: Double loop over unbalanced panel** - Next by thread:
**Re: st: Double loop over unbalanced panel** - Index(es):