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

st: Re: speeding up a loop


From   Christopher F Baum <baum@bc.edu>
To   statalist@hsphsun2.harvard.edu
Subject   st: Re: speeding up a loop
Date   Thu, 16 Sep 2004 14:02:40 -0400

On Sep 16, 2004, at 2:33, Friedrich wrote:

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?
As other posters note, perhaps you don't even need a loop. But one thing is worth remembering: in a large dataset, an if qualifier is hideously slow compared to an in qualifier. That is, if you go through the dataset once and figure out what the first and last record for each family will be, an in-qualifier using those numbers will run _much_ faster than an if-qualifier, which must examine every case to see whether it meets the conditions.

Kit

*
* 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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index