I have the below daily dataset to work with. I need a weighted average of all rt's for each time period (i.e., by date) weighted by their immediately previous period (lagged) mcap value. Therefore, data of rt's for each stock id is from begin June month onward and data on mcap is from last day of May (one period earlier) onward. The below data example has data on five stocks and actual dataset has data on about 5000 stocks.
If the same weighted average were to be calculated for monthly rt's then would the code require any modification?
If the same weighted average were to be calculated for monthly rt's then would the code require any modification?
Code:
* Example generated by -dataex-. For more info, type help dataex clear input float stock_id str54 stock int date double mcap float rt 1 "3M India Ltd." 15491 318.46 . 1 "3M India Ltd." 15494 310.07 -.026353024 1 "3M India Ltd." 15495 325 .04813806 1 "3M India Ltd." 15496 318.35 -.020450607 1 "3M India Ltd." 15497 320.94 .008138712 1 "3M India Ltd." 15498 310.97 -.03106353 1 "3M India Ltd." 15501 . . 1 "3M India Ltd." 15502 324.32 . 1 "3M India Ltd." 15503 321.05 -.010072942 1 "3M India Ltd." 15504 313.96 -.022105264 1 "3M India Ltd." 15505 318.8 .015428777 1 "3M India Ltd." 15508 324.32 .017314488 1 "3M India Ltd." 15509 332.26 .02448767 1 "3M India Ltd." 15510 331.76 -.0015256823 1 "3M India Ltd." 15511 331.19 -.001697793 1 "3M India Ltd." 15512 319.93 -.034013607 1 "3M India Ltd." 15515 326.74 .02130282 1 "3M India Ltd." 15516 325.67 -.0032752973 1 "3M India Ltd." 15517 318.24 -.02282947 1 "3M India Ltd." 15518 326.69 .02654867 1 "3M India Ltd." 15519 321.22 -.016724138 2 "3P Land Holdings Ltd." 15491 . . 2 "3P Land Holdings Ltd." 15494 . . 2 "3P Land Holdings Ltd." 15495 . . 2 "3P Land Holdings Ltd." 15496 . . 2 "3P Land Holdings Ltd." 15497 . . 2 "3P Land Holdings Ltd." 15498 . . 2 "3P Land Holdings Ltd." 15501 . . 2 "3P Land Holdings Ltd." 15502 4.28 . 2 "3P Land Holdings Ltd." 15503 . . 2 "3P Land Holdings Ltd." 15504 4.27 . 2 "3P Land Holdings Ltd." 15505 3.89 -.0886076 2 "3P Land Holdings Ltd." 15508 4.27 .09722222 2 "3P Land Holdings Ltd." 15509 4.68 .09704641 2 "3P Land Holdings Ltd." 15510 5.15 .1 2 "3P Land Holdings Ltd." 15511 5.04 -.02097902 2 "3P Land Holdings Ltd." 15512 4.77 -.05357143 2 "3P Land Holdings Ltd." 15515 4.95 .03773585 2 "3P Land Holdings Ltd." 15516 5.31 .07272727 2 "3P Land Holdings Ltd." 15517 5.83 .09830508 2 "3P Land Holdings Ltd." 15518 6.12 .04938272 2 "3P Land Holdings Ltd." 15519 5.98 -.02352941 3 "63 Moons Technologies Ltd." 15491 167.94 . 3 "63 Moons Technologies Ltd." 15494 170.37 .014459665 3 "63 Moons Technologies Ltd." 15495 166.41 -.023255814 3 "63 Moons Technologies Ltd." 15496 174.71 .0499232 3 "63 Moons Technologies Ltd." 15497 162.32 -.0709583 3 "63 Moons Technologies Ltd." 15498 157.97 -.026771653 3 "63 Moons Technologies Ltd." 15501 159.38 .008899677 3 "63 Moons Technologies Ltd." 15502 159.25 -.0008019246 3 "63 Moons Technologies Ltd." 15503 158.74 -.003210273 3 "63 Moons Technologies Ltd." 15504 158.48 -.001610306 3 "63 Moons Technologies Ltd." 15505 153.88 -.02903226 3 "63 Moons Technologies Ltd." 15508 169.22 .09966777 3 "63 Moons Technologies Ltd." 15509 166.66 -.01510574 3 "63 Moons Technologies Ltd." 15510 160.53 -.036809817 3 "63 Moons Technologies Ltd." 15511 159.76 -.00477707 3 "63 Moons Technologies Ltd." 15512 156.31 -.0216 3 "63 Moons Technologies Ltd." 15515 159.12 .017988553 3 "63 Moons Technologies Ltd." 15516 156.82 -.01445783 3 "63 Moons Technologies Ltd." 15517 143.27 -.08638957 3 "63 Moons Technologies Ltd." 15518 144.81 .010704728 3 "63 Moons Technologies Ltd." 15519 143.91 -.006178288 4 "A B B India Ltd." 15491 1042.38 . 4 "A B B India Ltd." 15494 1040.47 -.00182964 4 "A B B India Ltd." 15495 1042.38 .001832994 4 "A B B India Ltd." 15496 1046.4 .003862574 4 "A B B India Ltd." 15497 1036.23 -.009720535 4 "A B B India Ltd." 15498 1034.11 -.0020449897 4 "A B B India Ltd." 15501 1038.35 .0040983604 4 "A B B India Ltd." 15502 1102.77 .06204082 4 "A B B India Ltd." 15503 1102.77 0 4 "A B B India Ltd." 15504 1072.47 -.02747886 4 "A B B India Ltd." 15505 1112.52 .0373444 4 "A B B India Ltd." 15508 1118.88 .005714286 4 "A B B India Ltd." 15509 1117.18 -.0015151515 4 "A B B India Ltd." 15510 1076.28 -.0366085 4 "A B B India Ltd." 15511 1098.32 .02047647 4 "A B B India Ltd." 15512 1089.84 -.007717538 4 "A B B India Ltd." 15515 1087.3 -.0023332685 4 "A B B India Ltd." 15516 1078.4 -.008185538 4 "A B B India Ltd." 15517 1059.54 -.017488701 4 "A B B India Ltd." 15518 1069.29 .0092 4 "A B B India Ltd." 15519 1139.01 .06520016 5 "A B C India Ltd." 15491 2.15 . 5 "A B C India Ltd." 15494 . . 5 "A B C India Ltd." 15495 2.03 . 5 "A B C India Ltd." 15496 2 -.01234568 5 "A B C India Ltd." 15497 2.25 .125 5 "A B C India Ltd." 15498 1.9 -.15555556 5 "A B C India Ltd." 15501 . . 5 "A B C India Ltd." 15502 2.05 . 5 "A B C India Ltd." 15503 . . 5 "A B C India Ltd." 15504 2.2 . 5 "A B C India Ltd." 15505 2.03 -.07954545 5 "A B C India Ltd." 15508 2.23 .09876543 5 "A B C India Ltd." 15509 2.38 .06741573 5 "A B C India Ltd." 15510 2.55 .07368421 5 "A B C India Ltd." 15511 2.48 -.029411765 5 "A B C India Ltd." 15512 2.48 0 end format %td date

Comment