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, 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   "Gerard Solbrig" <>
To   <>
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

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

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.


-----Original Message-----
[] On Behalf Of ???
Sent: Sonntag, 30. September 2012 05:23
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,

At 2012-09-29 04:45:06,"Gerard Solbrig" <>
>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
>firm1		01jan2000	0	.			.
>firm1		05apr2004	0	.			.
>firm1		01nov2004	1	05may2004		30may2005
>firm1		10dec2004	0	.			.
>firm1		01jan2006	0	.			.
>firm2		30dec1999	1	03jul1999		27jul2000
>firm2		05jan2000	1	09jul1999		02aug2000
>firm2		06jun2000	0	.			.
>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.
>Gerard Solbrig
>*   For searches and help try:

*   For searches and help try:

*   For searches and help try:

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