Announcement

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

  • Create event time variable

    Hi, I have a time series of stock returns with multiple event date indicators, such as the following:

    date return event
    01jan2015 -0.2 0
    02jan2015 1.2 1
    03jan2015 0.3 0
    04jan2015 0.4 0
    05jan2015 -0.3 1
    06jan2015 -0.1 0

    I would like to create a new variable (timeToEvent) that takes the value of number of days from the event within a 3-day window around each event, where day 0 is each event day. The events are all spread apart by at least 3 days. For instance, it would look like this:

    date return event timeToEvent
    01jan2015 -0.2 0 -1
    02jan2015 1.2 1 0
    03jan2015 0.3 0 1
    04jan2015 0.4 0 -1
    05jan2015 -0.3 1 0
    06jan2015 -0.1 0 1

    How may I do this?



  • #2
    https://www.statalist.org/forums/for...treatment-time

    Comment


    • #3
      Here is one solution (which may not be the most efficient) using the community-contributed command rangestat, available from
      Code:
      net install rangestat.pkg
      The code:
      Code:
      * ignore the following lines in your own work; they only recreate your sample data
      
      clear
      input str9 date float return byte(event timeToEvent)
      "01jan2015" -0.2 0 -1
      "02jan2015" 1.2 1 0
      "03jan2015" 0.3 0 1
      "04jan2015" 0.4 0 -1
      "05jan2015" -0.3 1 0
      "06jan2015" -0.1 0 1
      end
      
      * copy the code from here onwards
      
      gen ddate = daily(date,"DMY") // this may not be needed if your original date variable is already in numeric format
      gen date_if_event = ddate*event
      rangestat (max) eventdate = date_if_event, interval(ddate -1 1)
      gen time_to_event = ddate - eventdate
      drop ddate date_if_event eventdate
      The output is:
      Code:
      . li, noobs ab(20) sep(0)
      
        +----------------------------------------------------------+
        |      date   return   event   timeToEvent   time_to_event |
        |----------------------------------------------------------|
        | 01jan2015      -.2       0            -1              -1 |
        | 02jan2015      1.2       1             0               0 |
        | 03jan2015       .3       0             1               1 |
        | 04jan2015       .4       0            -1              -1 |
        | 05jan2015      -.3       1             0               0 |
        | 06jan2015      -.1       0             1               1 |
        +----------------------------------------------------------+
      You can change the code appropriately if your date variable is not a string. For the future, it is best to provide sample data using the dataex command. That way such confusion about whether some variable is string or numeric, doesn't arise.
      Last edited by Hemanshu Kumar; 03 Dec 2022, 22:12.

      Comment


      • #4
        Hemanshu Kumar Thank you!

        Comment

        Working...
        X