The following is a complete different approach --- but I think it works (not
tested), and I bet that it is faster. It requires writing to the disk and
sorting, but there is no loop at all:
. sort hhid mlineno
. save 11, replace
. keep hhid lineno age
. ren lineno mlineno
. ren age mage
. sort hhid mlineno
. save 12, replace
. use 11
. merge hhid mlineno using 12, nokeep
many regards
uli
Friedrich Huebler 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
> }
If mlineo is either missing or a valid line number:
by hhid (mlineno): gen mage = age[mlineo[1]]
> 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!?
> Yahoo! Mail - 50x more storage than other providers!
> http://promotions.yahoo.com/new_mail
> *
> * 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/