Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Using egen and by efficiently when some observations are missing
From
Austin Nichols <[email protected]>
To
[email protected]
Subject
Re: st: Using egen and by efficiently when some observations are missing
Date
Thu, 22 Apr 2010 11:46:21 -0400
Paul Novosad <[email protected]> :
Inefficient in terms of time, for sure, as you are taking about 20
times as long as you need to for the computation. Try this on for
size:
clear
range country 1 100 100
expand 10000
g i=1 if uniform()<.1
g condition=uniform()<.2
set rmsg on
sort country
by country: egen tmp = count(i) if condition == 1
by country: egen count_i = max(tmp)
drop tmp
by country: gen long c_i = sum(i<. & condition==1)
by country: replace c_i = c_i[_N]
set rmsg off
compress
su
On Thu, Apr 22, 2010 at 11:24 AM, Paul Novosad <[email protected]> wrote:
> Dear list,
>
> It often takes me three lines to generate variables based on
> conditional group operations using egen. For example, I want to run
> some egen operation on a subset of the data, such as a count. But I
> want the count to exist even when the condition does not hold. I use
> the following:
>
> by country: egen tmp = count(i) if condition == 1
> by country: egen count_i = max(tmp)
> drop tmp
>
> I write code like this all over the place, and each time it makes my
> heart sink. It feels inefficient but I do not have another solution.
> Can someone recommend a more efficient practice?
>
> Thanks,
>
> Paul
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/