# RE: st: keeping dates which are n periods apart

 From n j cox To statalist@hsphsun2.harvard.edu Subject RE: st: keeping dates which are n periods apart Date Mon, 19 May 2008 18:15:52 +0100

Thanks for the clarification. The original problem was indeed more complicated than this one. Whether one problem is just a minor variation on another, or really different, is often hard to call.

Nick
n.j.cox@durham.ac.uk

Rajesh Tharyan

Hi nick,

Thanks very much for that, what you suggest is exactly what I needed. I
think it must been my wording of what I wanted to achieve that prompted
the complicated approach.

This was the original statement of the problem... In an earlier post

2. See if the next date is more than 730 days from it. if its less than
73o days away from the first date drop it.

3. If it is more than 730 days away keep it and

4. Now this date becomes the reference date and the difference between
this date and the next is calculated and so on

However, Scotts approach to use mod and your final suggestion was all that
was needed.

n j cox

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/