[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

From |
"n.j.cox@durham.ac.uk" <n.j.cox@durham.ac.uk> |

To |
"'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu> |

Subject |
Re: st: keeping dates which are n periods apart |

Date |
Mon, 19 May 2008 08:10:18 -0700 |

The word description of your problem -- concisely given in your title -- suggests a simple solution based on Scott Merryman's excellent suggestion of -mod(,)- as a basis for your problem. keep if mod(year, 3) == mod(1987, 3) gives the solution that you specify for the first example. The choice of 1987 reflects the fact that 1987 is the start of your first example. Note that this 1. Does not entail any attention to the panel structure of your data. 2. Ensures that the same years are selected for all panels when available. 3. Can be transferred to other periods by changing 3 to what is desired. 4. Can be modified easily for aspects of the problem not specified. For example, you could calculate mod(year, 3) and choose a start based on what kept most observations. Or, very irregular panels might be sampled by bysort id (year) : keep if mod(year, 3) == mod(year[1], 3) which for the second example below (different from the first) selects 1986(3)1995. Conversely, I don't understand why much more complicated solutions have been offered for this kind of problem. What am I missing? Nick n.j.cox@durham.ac.uk Rajesh Tharyan I have this ( a subset of the original dataset, the original dataset has about 6000 ids with an average of 6 years each) id year 4 1987 4 1988 4 1989 4 1990 4 1992 4 1993 4 1994 9 1987 9 1988 9 1989 9 1990 9 1992 9 1993 9 1994 I need to keep years if they are more than 2 years apart by company. In an earlier post http://www.stata.com/statalist/archive/2006-02/msg00952.html David Harrison suggested the following which works fine with one id local refdate = year[1] gen byte dropflag = 0 forvalues i = 2/`=_N' { if year[`i']-`refdate'<=2 { replace dropflag = 1 in `i' } else { local refdate = year[`i'] } } drop if dropflag drop dropflag However I am not able to do it by id. what I would want is this.. id year 4 1987 4 1990 4 1993 9 1987 9 1990 9 1993 I tried using the above code with levelsof and foreach as shown below but no luck. ******************* input id year 4 1987 4 1988 4 1989 4 1990 4 1992 4 1993 4 1994 9 1986 9 1987 9 1988 9 1989 9 1990 9 1992 9 1993 9 1994 9 1995 9 1996 9 1997 end levelsof id, local(levels) foreach l of local levels { qui sum year local refdate = year[1] gen byte dropflag = 0 forvalues i = 2/`=_N' { if year[`i']-`refdate'<=2 { replace dropflag = 1 in `i' } else { local refdate = year[`i'] } } drop if dropflag drop dropflag } list, table clean noobs ******************* which gives me id year 4 1987 4 1990 4 1993 9 1996 I tried a variation of the above as **Snip** levelsof id, local(levels) foreach l of local levels { qui sum year if id==`l' local refdate = r(min) local m= r(N) gen byte dropflag = 0 forvalues i = 2/`m'{ if year[`i']-`refdate'<=2 { replace dropflag = 1 in `i' } else { local refdate = year[`i'] } } drop if dropflag & id==`l' drop dropflag } **Snip** That gives me id year 4 1987 4 1990 4 1993 9 1995 9 1996 9 1997 Any pointers much appreciated * * For searches and help try: * http://www.stata.com/support/faqs/res/findit.html * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/ Privileged, confidential or patient identifiable information may be contained in this message. This information is meant only for the use of the intended recipients. If you are not the intended recipient, or if the message has been addressed to you in error, do not read, disclose, reproduce, distribute, disseminate or otherwise use this transmission. Instead, please notify the sender by reply e-mail, and then destroy all copies of the message and any attachments. * * For searches and help try: * http://www.stata.com/support/faqs/res/findit.html * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

- Prev by Date:
**-by:- is sweet [was: Re: Re: st: Creating a new variable with information from other observations]** - Next by Date:
**Re: Re: st: RE: Help with loops** - Previous by thread:
**RE: st: keeping dates which are n periods apart** - Next by thread:
**RE: st: keeping dates which are n periods apart** - Index(es):

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