Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Replace after egen with conditions


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Replace after egen with conditions
Date   Tue, 13 Nov 2012 00:22:49 +0000

This problem is discussed at moderate length in

Cox, N.J. 2011. Speaking Stata: Compared with ... Stata Journal 11: 305-314.

Here are three ways to solve it

1. A -replace- afterwards:

bysort date2: egen total_supply=total(supply) if dispatched=="Y" & big==1
bysort date2 (total_supply) : replace total_supply = total_supply[1]
if missing(total_supply)

2. Ensuring it isn't needed:

bysort date2 : egen total_supply = total(cond(dispatched == "Y" & big
== 1, supply, .))

3. Ensuring it isn't needed:

bysort date2 : egen total_supply = total(supply / (dispatched == "Y" &
big == 1))

#2 and #3 depend on two principles:

a. -egen-'s -total()- function works on expressions, which need not be
single variable names.

b. It also ignores missings.

#3 depends on dividing by a logical argument that can be 1 or 0. In
this case, supply / 1 = supply and supply / 0 == . (missing). the
latter being ignored, as above.

Nick


On Mon, Nov 12, 2012 at 10:12 PM, Miguel Olivo-Villabrille
<m.olivo@gmail.com> wrote:

> I have a panel data set, I am trying to compute totals by date for
> observations that match certain conditions:
>
> bys date2: egen total_supply=total(supply) if dispatched=="Y" & big==1
>
> this will give me what I want but it will only assign that total to
> observations that match the conditions. I am interested in producing a
> constant variable within dates that has that (total for observations
> that match the conditions) but assigned to all observations
> regardless. Any ideas how I might accomplish this?
> I tried
>
> bys date2: replace total_supply=total_supply[_n-1] if total_supply==.
>
> but that solves the problem for the bottom observations within date,
> and not the observations before the condition is met. Also, adding
>
> bys date2: replace total_supply=total_supply[_n+1] if total_supply==.
>
> will only change the one observation before the condition is met
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index