# st: Re: How to speed up loop

 From Friedrich Huebler To statalist@hsphsun2.harvard.edu Subject st: Re: How to speed up loop Date Wed, 15 Sep 2004 08:56:03 -0700 (PDT)

```Michael,

Thanks for the quick reply but lineno==mlineno is never true because
one cannot be one's own mother.

Friedrich

--- Michael Blasnik <michael.blasnik@verizon.net> wrote:
> assuming 1 mother per household, how about no loop at all:
>
> egen mage=max(age*(lineno==mlineno)), by(hhid)
>
> This would result in a 0 for any household without a mother listed,
> which
> you could easily change to missing or whatever you like.
>
> Michael Blasnik
> michael.blasnik@verizon.net
>
> ----- Original Message -----
> From: "Friedrich Huebler" <huebler@rocketmail.com>
> To: <statalist@hsphsun2.harvard.edu>
> Sent: Wednesday, September 15, 2004 11:33 AM
> Subject: st: How to speed up loop
>
>
> > I am looking for advice to speed up a loop. The loop determines
> > characteristics of other household members in household survey
> data
> > and is taken from this Stata FAQ:
> >
> > http://www.stata.com/support/faqs/data/members.html
> >
> > The data has the variables hhid (household ID), lineno (line
> number
> > of household member), age, and mlineno (mother's line number).
> >
> > hhid    lineno       age   mlineno
> >    1         1        32         .
> >    1         2        30         .
> >    1         3         5         2
> >    2         1        68         .
> >    2         2        41         1
> >    2         3        40         .
> >    2         4        17         3
> >    2         5        14         3
> >
> > The following code creates the variable mage with the mother's
> age.
> >
> > gen byte mage=.
> > sum lineno, meanonly
> > forvalues i = 1/`r(max)' {
> >   gen byte tag = 1 if lineno==`i'
> >   bysort hhid: egen byte iage = max(tag*age)
> >   replace mage=iage if mlineno==`i'
> >   drop tag iage
> >   }
> >
> > I tried changing -bysort hhid- to -by hhid- but the time needed
> for
> > the loop is the same. Running the loop -quietly- also makes no
> > difference. Can anything be done to make this loop go faster?
> >
> > Thank you,
> >
> > Friedrich Huebler

_______________________________
Do you Yahoo!?
Declare Yourself - Register online to vote today!
http://vote.yahoo.com
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```