Announcement

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

  • Merge via date

    Hi all,

    I am having trouble merging two datasets by date. I am using the below code found online and was told by Stata that I was using an old merge function, so I have tried the 1:m etc variations but none seem be yielding the results I wish (I did not attempt m:m as I've been informed this is useless). I have searched the forum for previous answers of which there are several but I am not yet able to make them work for my problem.

    One possible important point to note is my use of company_id. I have only one company but am using that as a convention from the code I copied.

    I am using Stata 13.0

    Many thanks in advance

    Code:
    use stockdata, clear
    sort company_id
    merge company_id using eventcount
    tab _merge
    keep if _merge==3
    drop _merge
    
    drop eventcount
    sort company_id date
    by company_id date: gen set=_n
    sort company_id set
    save stockdata2
    
    use eventdates, clear
    sort company_id
    by company_id: gen set=_n
    sort company_id set
    save eventdates2
    use stockdata2, clear
    merge company_id set using eventdates2
    tab _merge

    Stockdata

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(eurostoxx market_return) byte(ecbannouncement company_id) float set int event_date byte _merge float(date dif event_window estimation_window predicted_return id)
    391.08   .14 0 1   1 21300 3 21329 29 0 0 . 1
    384.47 -1.37 0 1   1 21300 3 21333 33 0 0 . 1
    385.49   .27 0 1   1 21300 3 21334 34 0 0 . 1
    383.06  -.63 0 1   1 21300 3 21335 35 0 0 . 1
    386.91  1.01 0 1   1 21300 3 21336 36 0 0 . 1
    388.11   .31 0 1   1 21300 3 21339 39 0 0 . 1
         .     . . 1 109 17443 2     .  . 0 0 . 1
         .     . . 1  41 19669 2     .  . 0 0 . 1
         .     . . 1  83 18276 2     .  . 0 0 . 1
         .     . . 1 110 17415 2     .  . 0 0 . 1
         .     . . 1 144 16379 2     .  . 0 0 . 1
         .     . . 1 223 14614 2     .  . 0 0 . 1
    end
    format %tdnn/dd/CCYY event_date
    format %td date
    Eventdates

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int event_date byte company_id
    21300 1
    21251 1
    21209 1
    21167 1
    21118 1
    21069 1
    Last edited by Toby Jones; 05 Jul 2018, 11:12.
Working...
X