Hi everybody,
this is the dataset I have:
I need to do the following: on 30th of June of every year (for the variable date; I have daily data), I need to create a new variable where (for every permno) I take the value of BE for which fyear (fiscal year) is equal to the value of year-1 if fyear is the same as year. For example, for the day 30jun1997, I need the value of BE which is contained where fyear is 1996 if year==1996 for the same permno.
Next, I need to make the value of this new variable the same for one year for every permno, for example with the previous case: the newvariable will have the same value as for 30jun1997 from 01july1197 until 29june1998.
I come from other programming languages where I can switch values from one entry to another with another logic, but in the first step I am completely lost. As for the second step, I have a guess about using something like
by permno year (date), sort: egen newvar=BE[_181]
to identify the 30th of June of every year but I am not sure about its validty, especially because there are some leap year (I think in that case
by permno year (date), sort: egen newvar=BE[_181]
by permno year (date), sort: egen newvar=BE[_182] if _N==366
would do the job. Correction: this doe not work since I have gaps in dates
Thank you in advance for your help
this is the dataset I have:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input long permno float date byte(dunemp dinflation dfomc duninfl dunfomc dinflfomc dall) float(MEt Exret) double firstbeta int fyear float(BE month day year) 10000 9502 0 0 0 0 0 0 0 . . . . . 1 6 1986 10000 9503 0 0 0 0 0 0 0 9430 . . . . 1 7 1986 10000 9504 1 0 0 1 1 0 1 9200 -.02464 . . . 1 8 1986 10000 9505 0 0 0 0 0 0 0 9200 -.00025 . . . 1 9 1986 10000 9506 0 1 0 1 0 1 1 9200 -.00025 1.1092484682756043 . . 1 10 1986 10000 9509 0 0 0 0 0 0 0 9660 .04975 2.298013031125736 . . 1 13 1986 10000 9510 0 0 0 0 0 0 0 10120 .047369 2.6404900943526526 . . 1 14 1986 10000 9511 0 0 0 0 0 0 0 10580 .045205 2.5170627711997664 . . 1 15 1986 10000 9512 0 0 0 0 0 0 0 11040 .043228 2.5458446861374435 . . 1 16 1986 10000 9513 0 0 0 0 0 0 0 11040 -.00025 2.515478872206193 . . 1 17 1986 10000 9516 0 0 0 0 0 0 0 11040 -.00025 2.5479645918335634 . . 1 20 1986 10000 9517 0 0 0 0 0 0 0 11040 -.00025 2.5900722513319483 . . 1 21 1986 10000 9518 0 0 0 0 0 0 0 11040 -.00025 2.590091307954286 . . 1 22 1986 10000 9519 0 0 0 0 0 0 0 13800 .24975 4.238323536310995 . . 1 23 1986 10000 9520 0 0 0 0 0 0 0 15410 .116417 4.61989046462943 . . 1 24 1986 10000 9523 0 0 0 0 0 0 0 16330 .059451 4.542009632838356 . . 1 27 1986 10000 9524 0 0 0 0 0 0 0 16330 -.00025 3.577983687689484 . . 1 28 1986 10000 9525 0 0 0 0 0 0 0 15870 -.028419 3.345643072886398 . . 1 29 1986 10000 9526 0 0 0 0 0 0 0 16330 .028736 3.3384033787335996 . . 1 30 1986 10000 9527 0 0 0 0 0 0 0 16100 -.014335 2.6629858217408513 . . 1 31 1986 10000 9530 0 0 0 0 0 0 0 16100 -.00028 2.242670046026241 . . 2 3 1986 10000 9531 0 0 0 0 0 0 0 16100 -.00028 2.2925257893783257 . . 2 4 1986 10000 9532 0 0 0 0 0 0 0 16100 -.00028 2.2781528858670246 . . 2 5 1986 10000 9533 0 0 0 0 0 0 0 15410 -.043137 2.134287139225645 . . 2 6 1986 10000 9534 1 0 0 1 1 0 1 16100 .044496 2.171270512479075 . . 2 7 1986 10000 9537 0 0 0 0 0 0 0 15870 -.014566 1.9334764408724603 . . 2 10 1986 10000 9538 0 0 0 0 0 0 0 15870 -.00028 1.939770609657658 . . 2 11 1986 10000 9539 0 0 0 0 0 0 0 15525 -.022019 1.915189020778588 . . 2 12 1986 10000 9540 0 0 1 0 1 1 1 15525 -.00028 1.8056945274917433 . . 2 13 1986 10000 9541 0 0 0 0 0 0 0 15755 .014535 1.6968127687120975 . . 2 14 1986 10000 9545 0 0 0 0 0 0 0 14720 -.065973 1.0337260855333557 . . 2 18 1986 10000 9546 0 0 0 0 0 0 0 14490 -.015905 1.171833119512883 . . 2 19 1986 10000 9547 0 0 0 0 0 0 0 13570 -.063772 .8063214646307302 . . 2 20 1986 10000 9548 0 0 0 0 0 0 0 13570 -.00028 .7088089286924604 . . 2 21 1986 10000 9551 0 0 0 0 0 0 0 13340 -.017229 .7530157271945829 . . 2 24 1986 10000 9552 0 0 0 0 0 0 0 13110 -.017521 .80927400318237 . . 2 25 1986 10000 9553 0 0 0 0 0 0 0 11960 -.087999 .8339709423299075 . . 2 26 1986 10000 9554 0 0 0 0 0 0 0 11960 -.00028 .7471801568728254 . . 2 27 1986 10000 9555 0 0 0 0 0 0 0 11960 -.00028 .746391023098367 . . 2 28 1986 10000 9558 0 0 0 0 0 0 0 11960 -.0003 .7570288849195854 . . 3 3 1986 10000 9559 0 0 0 0 0 0 0 12650 .057392 .6768527315804056 . . 3 4 1986 10000 9560 0 0 0 0 0 0 0 12765 .008791 .6723175515926605 . . 3 5 1986 10000 9561 0 0 0 0 0 0 0 14260 .116817 .8185619060034663 . . 3 6 1986 10000 9562 1 0 0 1 1 0 1 16330 .144861 .8133942512517864 . . 3 7 1986 10000 9565 0 0 0 0 0 0 0 15410 -.056638 .7077004461695315 . . 3 10 1986 10000 9566 0 0 0 0 0 0 0 15870 .029551 .7431362334910612 . . 3 11 1986 10000 9567 0 0 0 0 0 0 0 15870 -.0003 .7255980446150595 . . 3 12 1986 10000 9568 0 0 0 0 0 0 0 16330 .028686 .7257069173776578 . . 3 13 1986 10000 9569 0 1 0 1 0 1 1 16330 -.0003 .6557522487622843 . . 3 14 1986 10000 9572 0 0 0 0 0 0 0 16330 -.0003 .6833345003259159 . . 3 17 1986 10000 9573 0 0 0 0 0 0 0 16330 -.0003 .6665680318182341 . . 3 18 1986 10000 9574 0 0 0 0 0 0 0 16330 -.0003 .6821551042998169 . . 3 19 1986 10000 9575 0 0 0 0 0 0 0 16330 -.0003 .6710956362213673 . . 3 20 1986 10000 9576 0 0 0 0 0 0 0 16330 -.0003 .6925084577181038 . . 3 21 1986 10000 9579 0 0 0 0 0 0 0 16330 -.0003 .6819270071988135 . . 3 24 1986 10000 9580 0 0 0 0 0 0 0 16330 -.0003 .6967366137751768 . . 3 25 1986 10000 9581 0 0 0 0 0 0 0 16330 -.0003 .644206038839203 . . 3 26 1986 10000 9582 0 0 0 0 0 0 0 16215 -.007342 .6015833880957242 . . 3 27 1986 10000 9586 0 0 0 0 0 0 0 16330 .006792 .6031738708775016 . . 3 31 1986 10000 9587 0 0 0 0 0 0 0 15870 -.028409 .753663032811027 . . 4 1 1986 10000 9588 0 0 1 0 1 1 1 15870 -.00024 .7563925380424335 . . 4 2 1986 10000 9589 0 0 0 0 0 0 0 15870 -.00024 .7606684775922606 . . 4 3 1986 10000 9590 1 0 0 1 1 0 1 15870 -.00024 .7491797652544406 . . 4 4 1986 10000 9593 0 0 0 0 0 0 0 15640 -.014733 .7775320796436982 . . 4 7 1986 10000 9594 0 0 0 0 0 0 0 15180 -.029652 .5158959110292793 . . 4 8 1986 10000 9595 0 0 0 0 0 0 0 15525 .022487 .5183757291944111 . . 4 9 1986 10000 9596 0 0 0 0 0 0 0 15295 -.015055 .4552078201849054 . . 4 10 1986 10000 9597 0 1 0 1 0 1 1 15180 -.007759 .46499216533997756 . . 4 11 1986 10000 9600 0 0 0 0 0 0 0 15065 -.007816 .44700151566497554 . . 4 14 1986 10000 9601 0 0 0 0 0 0 0 15065 -.00024 .4474550935339181 . . 4 15 1986 10000 9602 0 0 0 0 0 0 0 15065 -.00024 .3864226015052452 . . 4 16 1986 10000 9603 0 0 0 0 0 0 0 14950 -.007874 .3804404167195379 . . 4 17 1986 10000 9604 0 0 0 0 0 0 0 14950 -.00024 .3853140140302527 . . 4 18 1986 10000 9607 0 0 0 0 0 0 0 14950 -.00024 .37111661654700334 . . 4 21 1986 10000 9608 0 0 0 0 0 0 0 14950 -.00024 .38017850151959354 . . 4 22 1986 10000 9609 0 0 0 0 0 0 0 14950 -.00024 .38749844929549093 . . 4 23 1986 10000 9610 0 0 0 0 0 0 0 14950 -.00024 .38729041569503636 . . 4 24 1986 10000 9611 0 0 0 0 0 0 0 14950 -.00024 .3899418931550269 . . 4 25 1986 10000 9614 0 0 0 0 0 0 0 15065 .007452 .3899482330939451 . . 4 28 1986 10000 9615 0 0 0 0 0 0 0 14950 -.007874 .41745736301004216 . . 4 29 1986 10000 9616 0 0 0 0 0 0 0 15172 -.015625 .47779987837270305 . . 4 30 1986 10000 9617 0 0 0 0 0 0 0 15172 -.00023 .48128432425689033 . . 5 1 1986 10000 9618 1 0 0 1 1 0 1 15053.47 -.008043 .48651269267029884 . . 5 2 1986 10000 9621 0 0 0 0 0 0 0 14757.16 -.019915 .42229431037894466 . . 5 5 1986 10000 9622 0 0 0 0 0 0 0 14460.813 -.02031 .4355063788245779 . . 5 6 1986 10000 9623 0 0 0 0 0 0 0 14223.75 -.016623 .458035118274006 . . 5 7 1986 10000 9624 0 0 0 0 0 0 0 14223.75 -.00023 .45243000538632144 . . 5 8 1986 10000 9625 0 0 0 0 0 0 0 14342.28 .008103 .45323803161974063 . . 5 9 1986 10000 9628 0 0 0 0 0 0 0 16001.72 .115472 .3815510980908785 . . 5 12 1986 10000 9629 0 0 0 0 0 0 0 15527.594 -.02986 .4187316146539895 . . 5 13 1986 10000 9630 0 0 0 0 0 0 0 15764.656 .015037 .4230301085376582 . . 5 14 1986 10000 9631 0 0 0 0 0 0 0 15290.53 -.030305 .4989900217899599 . . 5 15 1986 10000 9632 0 1 0 1 0 1 1 15053.47 -.015734 .5230010360575719 . . 5 16 1986 10000 9635 0 0 0 0 0 0 0 14697.875 -.023852 .5253704058640784 . . 5 19 1986 10000 9636 0 0 0 0 0 0 0 14697.875 -.00023 .5073523267899698 . . 5 20 1986 10000 9637 0 0 1 0 1 1 1 13868.156 -.056682 .5392347670915105 . . 5 21 1986 10000 9638 0 0 0 0 0 0 0 13038.438 -.060059 .335202959975203 . . 5 22 1986 10000 9639 0 0 0 0 0 0 0 13156.97 .008861 .3381504815493877 . . 5 23 1986 10000 9643 0 0 0 0 0 0 0 13156.97 -.00023 .32177334053140066 . . 5 27 1986 10000 9644 0 0 0 0 0 0 0 12682.844 -.036266 .28011880935371014 . . 5 28 1986 end format %td date
I need to do the following: on 30th of June of every year (for the variable date; I have daily data), I need to create a new variable where (for every permno) I take the value of BE for which fyear (fiscal year) is equal to the value of year-1 if fyear is the same as year. For example, for the day 30jun1997, I need the value of BE which is contained where fyear is 1996 if year==1996 for the same permno.
Next, I need to make the value of this new variable the same for one year for every permno, for example with the previous case: the newvariable will have the same value as for 30jun1997 from 01july1197 until 29june1998.
I come from other programming languages where I can switch values from one entry to another with another logic, but in the first step I am completely lost. As for the second step, I have a guess about using something like
by permno year (date), sort: egen newvar=BE[_181]
to identify the 30th of June of every year but I am not sure about its validty, especially because there are some leap year (I think in that case
by permno year (date), sort: egen newvar=BE[_181]
by permno year (date), sort: egen newvar=BE[_182] if _N==366
would do the job. Correction: this doe not work since I have gaps in dates
Thank you in advance for your help
Comment