Announcement

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

  • Rolling sum based on a dummy and if condition

    I can't figure out how to implement the following for this dataset:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(ID datenr excessreturn shock_ROLL LLS TRS) int qdate float Date
    1  1            . 0 0 0 160 14612
    1  2   .024564743 0 0 0 160 14613
    1  3  -.013171673 0 0 0 160 14614
    1  4    .03032541 0 0 0 160 14615
    1  5   -.04618168 0 0 0 160 14616
    1  6   -.00510788 0 0 0 160 14619
    1  7 -.0045223236 0 0 0 160 14620
    1  8   .009564877 0 0 0 160 14621
    1  9    .06696749 0 0 0 160 14622
    1 10  -.030753136 0 0 0 160 14623
    1 11   -.10425854 0 0 0 160 14626
    1 12   .026067734 0 0 0 160 14627
    1 13 -.0019803047 0 0 0 160 14628
    1 14 -.0030441284 0 0 0 160 14629
    1 15    .01700306 0 0 0 160 14630
    end
    format %tq!Qq-CCYY qdate
    format %td Date


    ID and datenr_ are: identifier and time index (daily)

    The variable shock_ROLL is either 0 or 1 (it is 1 in about 1% of the cases).

    If shock_ROLL is 1 at t=n then I need to check whether the sum of 21 days of excess return (meaning until t=20) have been positive, starting at t=n-1 (the day before the shock_ROLL is 1). In this case the variable LLS should have a value of 1.

    I also need to check the number of such LLS within a quarter and count the number per quarter, which could be shown in a new variable with the counting result as a value for each datenr_ in a quarter.

    The story behind: in a case of a large stock price drop (like BP oil spill or such a thing) I want to see if the stock price recovers to the level before the shock within 21 trading days using the excess returns (this is stock return minus market return).
Working...
X