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: Looping within a subset under a certain condition


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Looping within a subset under a certain condition
Date   Sun, 30 Sep 2012 00:13:20 +0100

I had another look at this. I still don't understand your problem
exactly (e.g. why is the second obs at 5apr2004 considered in window),
but the technique here may help.

egen first_start = min(wind_start), by(firm_id)
egen last_end = max(wind_end), by(firm_id)

gen in_window = inrange(date, first_start, last_end)

egen all_0_in_window = min(in_window) if rep == 0, by(firm_id)

On the last line: on all <=> min, any <=> max, see

FAQ     . . Creating variables recording whether any or all possess some char.
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        2/03    How do I create a variable recording whether any
                members of a group (or all members of a group)
                possess some characteristic?
                http://www.stata.com/support/faqs/data/anyall.html

Nick

On Fri, Sep 28, 2012 at 9:45 PM, Gerard Solbrig
<gsolbrig@mail.uni-mannheim.de> wrote:
>
> I'm encountering a problem for which I seek your help.
>
> Let me start off with an example from my data (what I want it to look like
> in the end), before I explain my particular problem.
>
> firm_id date            rep     wind_start              wind_end
> rep_ins
>
> firm1           01jan2000       0       .                       .
> 0
> firm1           05apr2004       0       .                       .
> 1
> firm1           01nov2004       1       05may2004               30may2005
> .
> firm1           10dec2004       0       .                       .
> 1
> firm1           01jan2006       0       .                       .
> 0
> firm2           30dec1999       1       03jul1999               27jul2000
> .
> firm2           05jan2000       1       09jul1999               02aug2000
> .
> firm2           06jun2000       0       .                       .
> 1
>
> Each firm in my data has a 'firm_id'. Variable 'date' refers to an event
> date. The 'rep' dummy indicates the type of event.
> I set 'wind_start' and 'wind_end' as period around the event
> (-180days,+210days), in case it's a rep = 1 type event.
>
> Now, I would like the 'rep_ins' dummy to indicate (i.e., rep_ins = 1),
> whether the date of all other observations of this firm (where rep = 0) lies
> within the range determined by 'wind_start' and 'wind_end' (which is
> conditional upon the 'rep' dummy).
>
> I've come across looping over observations and tried to design a solution
> for this problem based on that, but failed to do so. I assume the solution
> also depends on sorting the data in a special way.
>
> Here's the first part of my .do-file:
>
> gen wind_start = date-180 if rep == 1
> gen wind_end = date+210 if rep == 1
> format wind_start %d
> format wind_end %d
> gsort +cusip6 +date +trandate
> gen rep_ins = 0 if rep != 1
>
> I tried to come up with a solution by adding variables 'per_start' and
> 'per_end' for all rep = 0:
>
> gen per_start = date-180 if rep == 0
> gen per_end = date+180 if rep == 0
> format per_start %d
> format per_end %d
>
> To mark the period within which the rep = 1 event can lie. Maybe this could
> contribute to finding a solution as well.
*
*   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