Announcement

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

  • Variable which is changing in the range (e.g. _n-20/_n+20)

    Hi there,

    I am desperately looking for way to solve this problem!
    I have a large dataset downloaded from CRSP containing shareprices and dates in order to run an Event study.
    I had download the data for a long time Periode and want to drop all data outside of the estimation window.
    Here Comes the Problem: Since some companies have more than one Event, I cannot just combine Company Name and date of the Event.
    the set looks as follows:

    Date Company Eventdate
    12.3.2000 XXX 0
    13.3.2000 XXX 0
    14.3.2000 XXX 1
    15.3.2000 XXX 0
    16.3.2000 XXX 0
    17.3.2000 XXX 0
    12.3.2000 VVV 0
    13.3.2000 VVV 0
    14.3.2000 VVV 0
    15.3.2000 VVV 0
    16.3.2000 VVV 0
    17.3.2000 VVV 1
    18.3.2000 VVV 0
    ....
    (about 3Mio obs, and many cases, where one Company has several eventdates)

    Now I Need to create a dummy variable to show whether an observation is in the estimation window.
    My idea was to generate a variable (estimation_window) by an if-syntax, checking whether this observation is in a certain range around the eventdate.

    gen estimation_window=1 if Eventdate=1 in _n+100/_n-100 ...such that the range is always 200 observations around the current observation.

    But this command is not working: invalid observation number
    Is there an other way to make the range dependent on the observation, I am currently in?

    I would be very grateful to get any advise!

    Thanks alot!!

    Raphael Gallen,
    Switzerland

    Last edited by Raphael Gallen; 21 Apr 2018, 06:33. Reason: changing range, range dependent on observation

  • #2
    I used now this code: gen estimation_wind = 1 if ceo_turnover_date_1[_n]==1 |ceo_turnover_date_1[_n+1]==1 |ceo_turnover_date_1[_n+2]==1...
    it is very long, but fits to the purpose

    Comment


    • #3
      You can do this using rangestat (from SSC). The following example shows how to tag observations if there is an event within +/- 3 days from the date of the observation:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input long Company float(Date Eventdate)
      1 14616 0
      1 14617 0
      1 14618 0
      1 14619 0
      1 14620 1
      1 14621 0
      1 14622 0
      1 14623 0
      1 14624 0
      1 14625 0
      2 14628 0
      2 14630 1
      2 14631 0
      2 14632 0
      2 14633 0
      2 14634 0
      end
      
      rangestat (max) Eventdate, interval(Date -3 3) by(Company)
      
      list, sepby(Company)
      and the results:
      Code:
      . list, sepby(Company)
      
           +---------------------------------------+
           | Company    Date   Eventd~e   Eventd~x |
           |---------------------------------------|
        1. |       1   14616          0          0 |
        2. |       1   14617          0          1 |
        3. |       1   14618          0          1 |
        4. |       1   14619          0          1 |
        5. |       1   14620          1          1 |
        6. |       1   14621          0          1 |
        7. |       1   14622          0          1 |
        8. |       1   14623          0          1 |
        9. |       1   14624          0          0 |
       10. |       1   14625          0          0 |
           |---------------------------------------|
       11. |       2   14628          0          1 |
       12. |       2   14630          1          1 |
       13. |       2   14631          0          1 |
       14. |       2   14632          0          1 |
       15. |       2   14633          0          1 |
       16. |       2   14634          0          0 |
           +---------------------------------------+

      Comment


      • #4
        Wow Robert, I did not find that function for days!
        Thanks alot!!

        Comment


        • #5
          See https://www.statalist.org/forums/for...dow-with-loops for a recent example with the same question and the same solution.

          (Strictly, rangestat is a command, not a function.)

          Comment

          Working...
          X