Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: speed question: -collapse- vs -egen-


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: speed question: -collapse- vs -egen-
Date   Mon, 28 Apr 2008 13:28:14 +0100

Michael of course is just giving example code. 

In many problems, you would need to take account not only of missings
(as here) but also of -if- and -in- restrictions. One of the reasons
that -egen- and -collapse- are slow is that, as Stas pointed out, they
check for -if- and -in- and do the right thing for you. 

Nick
[email protected] 

Michael Blasnik

You can gain some speed in regular Stata code by not generating a
separate 
variable just to count the number of non-missings:

bysort rep78: gen mean=sum(price)/sum(price<.)
by rep78: keep if _n==_N

On my machine, this reduces the time required for the corrected Stas
code from 
17.3 to 13.8 s.


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