I would appreciate help creating the variable new_Expected = implied_rate[_n-1] - implied rate[the final day of month - 1]. In other words, the final day of the previous month. For example, if the month is August (month==8), then the final day of month - 1 is the final day of month == 7 which is 31. However, the data for implied_rate is financial market data, so it is only for 5 days/week and not on holidays. This may create an issue if the final day of the month is on a weekend, as it will not show up in my data. For example, see rows 189 to 190. If the data does not go to the actual end of the month (ie. day != D), I would like the new_Expected to take the implied_rate from the last day of the month when markets were open (row 189 in the case of September 2000). Variable D indicates the number of days in the given month.
Here is my current code:
gen new_Expected = .
replace new_Expected = implied_rate[_n-1] - implied_rate[_n-1-day] if Change != . & day != 1
replace new_Expected = implied_rate[_n] - implied_rate[_n-1] if Change != . & day = 1
I originally thought that I could do implied_rate[_n-1-day], but this doesn't work given that there are not 30 days worth of data/month (because of weekends and holidays). I'm trying to calculate new_Expected as the implied rate of the day before a Change (where Change != .) - implied_rate on the last day of the previous month.
Thank you so much in advance for the help!!

Here is my current code:
gen new_Expected = .
replace new_Expected = implied_rate[_n-1] - implied_rate[_n-1-day] if Change != . & day != 1
replace new_Expected = implied_rate[_n] - implied_rate[_n-1] if Change != . & day = 1
I originally thought that I could do implied_rate[_n-1-day], but this doesn't work given that there are not 30 days worth of data/month (because of weekends and holidays). I'm trying to calculate new_Expected as the implied rate of the day before a Change (where Change != .) - implied_rate on the last day of the previous month.
Thank you so much in advance for the help!!
Comment