My current code is:
Code:
gen date = date(date_str,"YMD")
format %td date
tsset date
forvalues t=1(1)10 {
local days = `t' * 365
gen d`t'y_close = close - L`days'.close
}
forvalues t=1(1)10 {
di "`t' years return"
di "number of days in which return was negative or zero"
count if d`t'y_close <= 0
di "total number of non missing days"
count if d`t'y_close
}
1. Currently, I manually calculate the number of days (as the data is daily) as products of 365 days a year and number of years I'm interested in. Is there a more elegant way to do so? a command that decreases X years from a given date, and handles leap years etc. appropriately?
2. Sometimes, "5 years prior" to a specific date falls on a day which doesn't exist in the data as there's no trade on this day (sunday, holidays, etc.). Is there a way to create an expression (i thought using cond but I think it's gonna amount to lots of nesting conds), in which the logic is "if X years prior to given date is missing, go the smallest amount of days backward and use that date instead"?
Thank you
0 Response to How to compute difference in years between daily dates?
Post a Comment