Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Howto: "drop all following observations if all of them are positive"

    Dear All,
    I (yet again) have a weird filtering problem, thatI would like to solve in Stata. Consider the following representative data:


    Code:
     clear  
     ssc install dataex  
     input float (bondid date yield)
    1    1    1.2
    1    2    0.8
    1    3    1.1
    1    4    0.9
    1    5    1
    1    6    1.1
    1    7    1.2
    2    1    1
    2    2    1.1
    2    3    1.2
    2    4    1.3
    2    5    1.4
    2    6    1.5
    2    7    1.6
    2    8    1.7
    2    9    1.8
    3    1    1.2
    3    2    0.8
    3    3    1.1
    3    4    0.9
    3    5    1
    3    6    1.1
    3    7    1.2
    3    8    1.3
    3    9    1.4
    3    10    1.5
    3    11    1.6
    end
    
    bys bondid (date): gen YieldChange =( yield-yield[_n-1])/yield[_n-1]
    My Datastream yield data seems corrupted in that some bond yields start increasing exponentially towards maturity date (going into the gazillions). I assume the Datastream algorithm breaks down here. To avoid bias in the data I would like to drop all observations from where on the YieldChange is always positive for all following observations.

    In the example above, this would mean:
    • Bond 1: Drop 5-7
    • Bond 2: Drop all
    • Bond 3: Drop 5-11
    Whenever problems become recursive, I am a bit lost in Stata. Can anyone point me to useful commands in this respect?
    This is to be computed on 12,000 bonds and T= 4*365 so computationally efficiency may be important, but at first I would like to understand how to tackle something like this in Stata.

    Thanks in advance!

    Best,
    Last edited by Jannic Cutura; 08 May 2017, 10:39.

  • #2
    Always positive here seems to mean yield >1. (Like most of the most active members I do not deal with finance data.)

    I suggest reversing time:

    Code:
    gsort bondid -date
    by bondid : drop if sum(yield > 1) == 0

    Compare the FAQ http://www.stata.com/support/faqs/da...t-occurrences/
    Last edited by Nick Cox; 08 May 2017, 10:36.

    Comment


    • #3
      Nick Cox Thanks! Let me try this. What I meant was that if YieldChange is positive and for all subsequent obsevrations positive as well, then drop the observations. but your code should be easily adjustable -- let me try.

      Comment

      Working...
      X