Bookmark and Share

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


From   "Gerard Solbrig" <[email protected]>
To   <[email protected]>
Subject   RE: st: Looping within a subset under a certain condition
Date   Sun, 30 Sep 2012 11:10:43 +0200

Pengpeng, thanks for the reply.

What you pointed out is somewhat of a special case I included in the
example. 

For my understanding, as Stata works its way through all observations in
every group of 'firm_id' and encounters a rep = 1 case, it'll just check on
whether all other observations in that group (starting with the next
observation, `i'+1) have a 'trandate' within the the range 'wind_start' and
'wind_end'. 

Thus, if it encounters another rep = 1 case (which are generally missing a
'trandate'), it should set 'rep_ins' = 0 for that case and continue to go
through all other observations. I try to correct that by including the line
'replace rep_ins = . if rep == 1' after my loop.

As for the possibility that there are multiple rep = 1 cases for one firm:
ideally, the loop would indicate for each rep = 0 case within which of these
different and possibly overlapping windows the observation's 'trandate'
lies. But I assume that would add a whole lot more complications to the
code. So for now, I'm just trying to get it to work so that it set's rep_ins
= 1 if any observation's 'trandate' lies within the range 'wind_start'
'wind_end' of one or more rep = 1 cases for that firm.

Best, 
Gerard


-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of ???
Sent: Sonntag, 30. September 2012 05:23
To: [email protected]
Subject: Re:st: Looping within a subset under a certain condition

Hi Gerard,

I thought your problem might be very similar as what I encountered before. 

You said that you attempted to indicate whether the date of all other
observations of this firm (where rep = 0) lay within the range determined by
'wind_start' and 'wind_end', but in your data, the firm2 had two set of
'wind_start' and 'wind_end', (03jul1999 - 27jul2000 and 09jul1999 -
02aug2000), so which one should be used in the determination? 

Thank you. 
Best wishes,

Pengpeng
At 2012-09-29 04:45:06,"Gerard Solbrig" <[email protected]>
wrote:
>Dear Statalist-Members,
>
>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.
>
>All input is welcome. Thanks in advance.
>Best,
>Gerard Solbrig
>
>*
>*   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/

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


*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index