I need to create a value-weighted average over a 2-day window, today and the day after.
The variable is "profit" and the weights are "lme". The resulting value-weighted variable is "INDEX_2d"
I used the following code:
rangestat will measure the variable INDEX_2d here using 2 days if these are two subsequent days in my data. The problem I am facing is that the variable INDEX_2d will not be calculated for 2 days on most Fridays as I do not have observations on Saturdays in most years in my sample.
My question:
1-How can I adjust the code so that INDEX_2d calculated on Fridays will be measured over the values of "Friday and Monday in the next week" if there are no observations on Saturday?
An example of the data is below (note that day_of_week is the day of week variable created using dow() in Stata":
The variable is "profit" and the weights are "lme". The resulting value-weighted variable is "INDEX_2d"
I used the following code:
Code:
gen double wm_num = profit * lme rangestat (mean) mean=profit (sum) wm_denom=lme (sum) wm_numer=wm_num, int(date 0 1) gen double INDEX_2d = wm_numer/wm_denom
My question:
1-How can I adjust the code so that INDEX_2d calculated on Fridays will be measured over the values of "Friday and Monday in the next week" if there are no observations on Saturday?
An example of the data is below (note that day_of_week is the day of week variable created using dow() in Stata":
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input double(day_of_week profit lme) 4 .0022047890722101406 711.9156372038269 4 .0022047890722101406 543.3194490831374 4 .0022047890722101406 7341.637578514405 4 .0022047890722101406 398.12341038471277 4 .0022047890722101406 183.10185659065246 4 .0022047890722101406 20.565780587406152 4 .0022047890722101406 1266.9609187219248 2 .07601980404716957 3618.476323240204 2 .07601980404716957 3013.7244912120805 2 .07601980404716957 727.2898823281866 2 .07601980404716957 146.46254176231378 2 .07601980404716957 46549.321507796645 2 .07601980404716957 831.575259840698 2 .07601980404716957 613.6025419473262 2 .07601980404716957 766.2193993235778 2 .07601980404716957 9449.638920065481 2 .07601980404716957 2681.182487020269 3 .012440886544283988 175.48500537872314 3 .012440886544283988 31.1685009598732 3 .012440886544283988 10.159600287342073 3 .012440886544283988 51.47269101625443 3 .012440886544283988 431.4772886264036 3 .012440886544283988 10.58813966308594 3 .012440886544283988 7.682400300598147 3 .012440886544283988 392.2163397516251 3 .012440886544283988 90.3589097388649 3 .012440886544283988 999.4461630648038 3 .012440886544283988 100.0057583646776 3 .012440886544283988 29.64872960342882 3 .012440886544283988 6005.390086240077 3 .012440886544283988 2209.55931459961 3 .012440886544283988 143.94519865946768 3 .012440886544283988 8945.173672529752 3 .012440886544283988 185.72652167747492 3 .012440886544283988 290.90629080173494 3 .012440886544283988 109.52676824779337 4 -.061068619779393166 4352.040138778684 4 -.061068619779393166 5.688039960107801 4 -.061068619779393166 18.82560092010499 4 -.061068619779393166 97.34000205993652 4 -.061068619779393166 509.32002449035645 4 -.061068619779393166 51.61805962509152 4 -.061068619779393166 63796.92068481445 4 -.061068619779393166 298.885453196106 1 -.023447644189903524 71.01658210460664 1 -.023447644189903524 4.025501194065669 1 -.023447644189903524 90.68687862693787 1 -.023447644189903524 136.74045178251254 1 -.023447644189903524 55.48939808902742 1 -.023447644189903524 63.97050154209137 1 -.023447644189903524 222.71483975378032 1 -.023447644189903524 28.86320038490294 1 -.023447644189903524 15.527450164842605 1 -.023447644189903524 181.33850288391113 1 -.023447644189903524 48.07600021362305 1 -.023447644189903524 240.32055972404487 1 -.023447644189903524 15.86815940555573 1 -.023447644189903524 74.95950444030768 1 -.023447644189903524 433.98721329002365 1 -.023447644189903524 17610.71413934324 1 -.023447644189903524 32.81908027641293 2 -.2161305421145078 1959.90388602257 2 -.2161305421145078 3537.586578459479 2 -.2161305421145078 665.7957794902031 2 -.2161305421145078 1373.5815437182246 2 -.2161305421145078 492.47760495414695 2 -.2161305421145078 63.325501799583435 2 -.2161305421145078 7344.087586425478 2 -.2161305421145078 12.264480772991192 2 -.2161305421145078 60.80117278902435 2 -.2161305421145078 670.2494553552242 2 -.2161305421145078 177.12407360450743 2 -.2161305421145078 148.0038986526488 2 -.2161305421145078 9471.08540616883 2 -.2161305421145078 1085.5815153051008 2 -.2161305421145078 1768.925487185974 3 .015310256189018498 153806.11037007533 3 .015310256189018498 159.8470020561217 3 .015310256189018498 12129.189087381586 3 .015310256189018498 554.488140872345 3 .015310256189018498 62590.77861785889 3 .015310256189018498 36839.00133413728 3 .015310256189018498 418.5895964187621 3 .015310256189018498 1194.9026238739025 3 .015310256189018498 11130.27061756805 3 .015310256189018498 756.938714970398 3 .015310256189018498 1769.112088676833 3 .015310256189018498 874.5053886123642 3 .015310256189018498 86.91149568557739 3 .015310256189018498 64486.60707391659 3 .015310256189018498 26433.892579855165 3 .015310256189018498 5942.193603992462 3 .015310256189018498 82.04199955034255 3 .015310256189018498 515.430379104233 3 .015310256189018498 172.92013699302697 3 .015310256189018498 1063.088165829773 3 .015310256189018498 166.66747447372472 3 .015310256189018498 8031.518099713139 3 .015310256189018498 30087.831573486328 3 .015310256189018498 1251.4882093553751 end
Comment