Dear All, Soppose I have the following data (a related thread is here):
I have trading data (on daily prices) for three years (shown only about 90 observations per year here), where `ymd" and "year" are dates and years, and `wti' is the prices. I used (suggested by Nick Cox) the following code
to generate `etime' (for event time), which is equal to 0 on February 24 (for every year), and previous trading day (i.e., with data on `wti') being -1, and next trading day being 1, and so on (please see `etime' in year 2020 for what I want.). However, in this new case, there were no trades on February 24 in year 2019, and 2018. In such case, how can I use the most neighboring trade day to set `etime'=0. For instance, in year 2018, I set Febrary 23 to be etime=0, and in year 2019, I set Febrary 25 to be etime=0. Thanks.
Code:
* Example generated by -dataex-. For more info, type help dataex clear input int ymd double wti float(year base etime) 21216 65.8 2018 0 1 21217 65.45 2018 0 2 21220 64.15 2018 0 3 21221 63.39 2018 0 4 21222 61.79 2018 0 5 21223 61.15 2018 0 6 21224 59.2 2018 0 7 21227 59.29 2018 0 8 21228 59.19 2018 0 9 21229 60.6 2018 0 10 21230 61.34 2018 0 11 21231 61.68 2018 0 12 21235 61.9 2018 0 13 21236 61.68 2018 0 14 21237 62.77 2018 0 15 21238 63.55 2018 0 16 21241 63.91 2018 0 17 21242 63.01 2018 0 18 21243 61.64 2018 0 19 21244 60.99 2018 0 20 21245 61.25 2018 0 21 21248 62.57 2018 0 22 21249 62.6 2018 0 23 21250 61.15 2018 0 24 21251 60.12 2018 0 25 21252 62.04 2018 0 26 21255 61.36 2018 0 27 21256 60.71 2018 0 28 21257 60.96 2018 0 29 21258 61.19 2018 0 30 21259 62.34 2018 0 31 21262 62.06 2018 0 32 21263 63.4 2018 0 33 21264 65.17 2018 0 34 21265 64.3 2018 0 35 21266 65.88 2018 0 36 21269 65.55 2018 0 37 21270 65.25 2018 0 38 21271 64.38 2018 0 39 21272 64.94 2018 0 40 21581 55.26 2019 0 1 21584 54.56 2019 0 2 21585 53.66 2019 0 3 21586 54.01 2019 0 4 21587 52.64 2019 0 5 21588 52.72 2019 0 6 21591 52.41 2019 0 7 21592 53.1 2019 0 8 21593 53.9 2019 0 9 21594 54.41 2019 0 10 21595 55.59 2019 0 11 21599 56.09 2019 0 12 21600 56.92 2019 0 13 21601 56.96 2019 0 14 21602 57.26 2019 0 15 21605 55.48 2019 0 16 21606 55.5 2019 0 17 21607 56.94 2019 0 18 21608 57.22 2019 0 19 21609 55.8 2019 0 20 21612 56.59 2019 0 21 21613 56.56 2019 0 22 21614 56.22 2019 0 23 21615 56.66 2019 0 24 21616 56.07 2019 0 25 21619 56.79 2019 0 26 21620 56.87 2019 0 27 21621 58.26 2019 0 28 21622 58.61 2019 0 29 21623 58.52 2019 0 30 21626 59.09 2019 0 31 21627 59.03 2019 0 32 21628 59.83 2019 0 33 21629 59.98 2019 0 34 21630 59.04 2019 0 35 21633 58.82 2019 0 36 21634 59.94 2019 0 37 21635 59.41 2019 0 38 21636 59.3 2019 0 39 21637 60.14 2019 0 40 21948 50.11 2020 15 -14 21949 49.61 2020 15 -13 21950 50.75 2020 15 -12 21951 50.95 2020 15 -11 21952 50.32 2020 15 -10 21955 49.57 2020 15 -9 21956 49.94 2020 15 -8 21957 51.17 2020 15 -7 21958 51.42 2020 15 -6 21959 52.05 2020 15 -5 21963 52.05 2020 15 -4 21964 53.29 2020 15 -3 21965 53.78 2020 15 -2 21966 53.38 2020 15 -1 21969 51.43 2020 15 0 21970 49.9 2020 15 1 21971 48.73 2020 15 2 21972 47.09 2020 15 3 21973 44.76 2020 15 4 21976 46.75 2020 15 5 end format %tdnn/dd/CCYY ymd
Code:
bysort year (ymd): gen base = sum(_n * (ymd == mdy(2, 24, year))) by year: replace base = base[_N] by year: gen etime = _n - base
Comment