Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: How to speed up loop


From   Ulrich Kohler <[email protected]>
To   [email protected]
Subject   st: Re: How to speed up loop
Date   Wed, 15 Sep 2004 18:47:37 +0200

Nick's approach is much better than mine. But probably with an if qualifier:

. by hhid : gen mage = age[mlineno] if mlineno < .

uli

Nick Cox wrote:
> Looks like
>
> by hhid : gen mage = age[mlineno]
>
> to me. No loops, no -merge-.
>
> Nick
> [email protected]
>
> Friedrich Huebler
>
> > I appreciate the responses that I received so far but realize that my
> > initial message was not precise enough.
> >
> > The variable mlineno lists the line number of a household member's
> > mother. In household 1 in my example data, household member 2 is the
> > mother of household member 3. In household 2, 1 is the mother of 2,
> > and 3 is the mother of 4 and 5. I would like to create a variable
> > that lists the age of each household member's mother. The result
> > should look like this:
> >
> > hhid    lineno       age   mlineno      mage
> >    1         1        32         .         .
> >    1         2        30         .         .
> >    1         3         5         2        30
> >    2         1        68         .         .
> >    2         2        41         1        68
> >    2         3        40         .         .
> >    2         4        17         3        40
> >    2         5        14         3        40
> >
> > The loop that I am using to create the variable mage works perfectly
> > but it is very slow with a large number of observations.
> >
> > Friedrich
> >
> > --- Friedrich Huebler <[email protected]> wrote:
> > > 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?
>
> *
> *   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/

-- 
[email protected]
+49 (030) 25491-361


*
*   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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index