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: Nearest value in a not xtset-able panel dataset


From   Francesco <[email protected]>
To   [email protected]
Subject   Re: st: Nearest value in a not xtset-able panel dataset
Date   Wed, 28 Mar 2012 20:01:38 +0200

Dear Nick,

How could you be so fast ! ;-)
Nice cascade effect, indeed, thanks for your help

1) I have to modify some things, but you have found the main idea I guess
2) Ties are not a problem for my work, because eventual ties would be
too far away from the previous (or next) special date in any case.

Many thanks again,
Have a nice day

Best,

On 28 March 2012 19:35, Nick Cox <[email protected]> wrote:
> 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/

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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index