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: Nearest value in a not xtset-able panel dataset
From
Nick Cox <[email protected]>
To
[email protected]
Subject
Re: st: Nearest value in a not xtset-able panel dataset
Date
Wed, 28 Mar 2012 18:35:13 +0100
The previous -special_date- (speak softly...) is
gen previous = date if special_date
bysort id (date) : replace previous = previous[_n-1] if missing(previous)
The following -special_date- is got by reversing time temporarily
gen following = date if special_date
gsort id -date
by id : replace following = following[_n-1] if missing(following)
The closer of the two is thus
gen nearer = cond((date - previous) < (following - date), previous, following)
Your difference variable is given by something like
gen diff = date - nearer
The key idea here is explained at
http://www.stata.com/support/faqs/data/missing.html
But
1. Note: None of this code is tested. Some terms may need negation.
2. What about ties, i.e. (previous special date to present date =
present date to following special date)?
Nick
On Wed, Mar 28, 2012 at 6:12 PM, Francesco <[email protected]> wrote:
> Dear Statalist,
>
> I have a simple algorithmic problem that I was unable to solve so far...
> Let us assume that I have the following dataset, where special_date
> identifies ... a special date ;-)
>
> ID DATE SPECIAL_DATE DIFFERENCE
> Jack 1 0 missing
> Jack 4 0 -1
> Jack 4 0 -1
> Jack 5 1 0
> Jack 7 0 missing
> Jack 9 0 missing
> Jack 10 0 missing
> Jack 11 0 -1
> Jack 12 1 0
> Jack 13 0 +1
> Jack 13 0 +1
> Jack 17 0 missing
>
> My question is :
>
> How could I obtain the variable DIFFERENCE that gives me the
> difference between the current observation's date and the nearest
> special date if this distance is less than a threshold N (N=1 in the
> example)?
> In any case N is much smaller than the periodicity of special date.
> For example N could be 10 days and Special date could occur only one
> time each quarter.
>
> Could you please help me (if possible)?
>
> Many, many thanks in advance,
>
> Best,
>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/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/statalist/faq
* http://www.ats.ucla.edu/stat/stata/