Dear Stata Users,
I have the data below, where "gvkey" is a firm ID, "begin" is a variable that specifies when the year starts and "end" specified when year ends. "final_date" includes the starting and ending dates of a year. I need to calculate the mean of "ret" from beginning date till ending date (both date should be inluded in calculation). If there are missing observations as at the beginning (no beginning date) or at the end (no end date) this observations should be left untreated. Please, help me with this issue.
I have the data below, where "gvkey" is a firm ID, "begin" is a variable that specifies when the year starts and "end" specified when year ends. "final_date" includes the starting and ending dates of a year. I need to calculate the mean of "ret" from beginning date till ending date (both date should be inluded in calculation). If there are missing observations as at the beginning (no beginning date) or at the end (no end date) this observations should be left untreated. Please, help me with this issue.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input double ret str6 gvkey float(end begin final_date) -.1849214732646942 "001004" . . . -.21935483813285828 "001004" . . . .17768594622612 "001004" . . . .07143859565258026 "001004" . . . .03947368264198303 "001004" 472 . 472 .1487341821193695 "001004" . 473 473 -.06787879019975662 "001004" . . . .014836795628070831 "001004" . . . -.15789473056793213 "001004" . . . -.0681944414973259 "001004" . . . -.01123595517128706 "001004" . . . .08712121099233627 "001004" . . . -.009198606014251709 "001004" . . . .3427561819553375 "001004" . . . -.2973684072494507 "001004" . . . -.09228464215993881 "001004" . . . -.07883817702531815 "001004" 484 . 484 -.13513512909412384 "001004" . 485 485 .007083333563059568 "001004" . . . -.0625 "001004" . . . .01666666753590107 "001004" . . . .051147542893886566 "001004" . . . -.13089005649089813 "001004" . . . .21686747670173645 "001004" . . . .20990096032619476 "001004" . . . -.1046740785241127 "001004" . . . -.06176471710205078 "001004" . . . -.11324456334114075 "001004" . . . .24666078388690948 "001004" 496 . 496 .22142860293388367 "001004" . 497 497 -.028947362676262856 "001004" . . . .03268758952617645 "001004" . . . -.5304806232452393 "001004" . . . -.048064958304166794 "001004" . . . .07236838340759277 "001004" . . . .1055215522646904 "001004" . . . -.04273028299212456 "001004" . . . -.13953490555286407 "001004" . . . .45540544390678406 "001004" . . . .18291543424129486 "001004" . . . -.1000785082578659 "001004" 508 . 508 -.10839159041643143 "001004" . 509 509 -.1406862735748291 "001004" . . . -.29061785340309143 "001004" . . . -.23387093842029572 "001004" . . . -.1768420934677124 "001004" . . . .4066495895385742 "001004" . . . -.06363634765148163 "001004" . . . -.07572812587022781 "001004" . . . -.06512613594532013 "001004" . . . -.15056176483631134 "001004" . . . .026455065235495567 "001004" . . . .15979377925395966 "001004" 520 . 520 .5688889026641846 "001004" . 521 521 .1473088413476944 "001004" . . . -.011111129075288773 "001004" . . . .0012484679464250803 "001004" . . . .35910212993621826 "001004" . . . -.04036693647503853 "001004" . . . .42925429344177246 "001004" . . . -.10702337324619293 "001004" . . . -.03220976144075394 "001004" . . . -.04953562840819359 "001004" . . . -.17345277965068817 "001004" . . . -.05615760758519173 "001004" 532 . 532 .1847599595785141 "001005" . 533 533 -.0837005078792572 "001005" . . . .028846172615885735 "001005" . . . .16355140507221222 "001005" . . . -.05622488632798195 "001005" . . . .16340425610542297 "001005" . . . -.003657658351585269 "001005" . . . -.144640251994133 "001005" . . . -.042918454855680466 "001005" . . . .21973101794719696 "001005" . . . .08308817446231842 "001005" . . . .08893424272537231 "001005" 544 . 544 -.02057361975312233 "001005" . 545 545 .14385737478733063 "001005" . . . -.020589813590049744 "001005" . . . -.023863639682531357 "001005" . . . -.07275901734828949 "001005" . . . .31450095772743225 "001005" . . . .14374403655529022 "001005" . . . -.005010473076254129 "001005" . . . .06210656836628914 "001005" . . . .12524694204330444 "001005" . . . -.06320222467184067 "001005" . . . -.09745128452777863 "001005" 556 . 556 -.07682725787162781 "001005" . 557 557 .0652272030711174 "001005" . . . -.05785476416349411 "001005" . . . .06857914477586746 "001005" . . . .09228190779685974 "001005" . . . .021121321246027946 "001005" . . . .09778113663196564 "001005" . . . .02055499702692032 "001005" . . . -.023833533748984337 "001005" . . . -.05226961523294449 "001005" . . . .10812777280807495 "001005" . . . end format %tm end format %tm begin format %tm final_date
Comment