Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Calculate market equity and match it to monthly returns in the next year

    dear statalisters,

    I am brand new to stata and regarding a classwork at my university I've got to calculate market equity (shares price x shares outstanding) from a dataset which looks like this:
    prc vol ret shrout monthdt
    35 39 1259 1972m4
    33 298 -.057142857 1259 1972m5
    28.25 271 -.143939391 1259 1972m6
    26 279 -.079646021 1259 1972m7
    21.75 322 -.163461536 1259 1972m8
    21.25 205 -.022988506 1259 1972m9
    20.875 114 -.01764706 1259 1972m10
    19.875 420 -.047904193 1259 1972m11
    25 840 .257861644 1259 1972m12
    20 691 -.200000003 1259 1973m1
    20.5 401 .025 1259 1973m2
    18.5 380 -.097560972 1259 1973m3
    15.5 121 -.162162155 1259 1973m4
    -14.125 306 -.088709675 1259 1973m5
    -14.437 203 .022123894 1259 1973m6
    15.125 215 .047619049 1259 1973m7
    -12.81 145 -.15289256 1259 1973m8
    15.5 346 .209756091 1259 1973m9
    14.5 234 -.062903225 1259 1973m10
    -11.18 297 -.228448272 1259 1973m11
    10 341 -.106145248 1259 1973m12
    -10.875 67 .090000004 1259 1974m1
    9.875 40 -.091954023 1259 1974m2
    11.87 188 .202531651 1259 1974m3
    10 145 -.15578948 1259 1974m4
    -10.43 132 .043749999 1259 1974m5
    9.625 161 -.077844314 1259 1974m6


    However, the market equity needed has to be from june (t-1) and then matched to monthly returns from July (t) through June (t+1), i.e. after generating the variable for market equity and copying every june (t-1) observation for the entire year I would then need to lag it 13 months so that it matches the returns starting from July (t) for a year.
    Nevertheless that data is vastly extensive and usually not consistent because I have to also take account the id of the security that sometimes doesn't report observations for a consistent time frame.


    This is my codeline so far:
    replace prc = abs(prc)

    generate month=month(date)
    generate maeq = prc*shrout if month==6

    replace maeq = maeq[_n-1] if missing(maeq)
    generate lagmaeq = maeq[_n-13]



    the code works well, however I don't get the desired results and I think that I am not taking into account the id of the security and just lagging the maeq for june. I guess that is a problem because then other securities could be assigned with maeq from the previous security.
    I am struggling on this problem for an entire day now. How do you think I could account for this issue?


    I really appreciate any help you could give me,


    Best,

    Thom


    Last edited by Thomas Schneider; 13 May 2016, 09:47.

  • #2
    Your example data is incomplete. There is no date variable or company identifier in it. Further, the example cannot be directly copied to Stata. So use the dataex program to generate a sample and then repost your data.
    Code:
    ssc install dataex
    dataex in 1/100
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

    Comment


    • #3
      Dear Attaullah,

      sorry for the sample last post. The company identifier in my sample is permno. Here I've copied the data using the code you provided:



      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input double permno long date double(shrcd permco) str8 cusip double(nwperm prc vol ret shrout) float(monthdt month maeq lagmaeq)
      10000  9496  . 7952 "68391610" .        .    .                     .    . 311 12         .         .
      10000  9527 10 7952 "68391610" .    4.375 1771                     . 3680 312  1         .         .
      10000  9555 10 7952 "68391610" .     3.25  828    -.2571428716182709 3680 313  2         .         .
      10000  9586 10 7952 "68391610" .   4.4375 1078    .36538460850715637 3680 314  3         .         .
      10000  9616 10 7952 "68391610" .        4  957   -.09859155118465424 3793 315  4         .         .
      10000  9646 10 7952 "68391610" . 3.109375 1074            -.22265625 3793 316  5         .         .
      10000  9677 10 7952 "68391610" .  3.09375 1069  -.005025125574320555 3793 317  6 11734.594         .
      10000  9708 10 7952 "68391610" .  2.84375 1163   -.08080808073282242 3793 318  7 11734.594         .
      10000  9737 10 7952 "68391610" .  1.09375 3049    -.6153846383094788 3793 319  8 11734.594         .
      10000  9769 10 7952 "68391610" .  1.03125 3551   -.05714285746216774 3793 320  9 11734.594         .
      10000  9800 10 7952 "68391610" .   .78125 1903   -.24242424964904785 3843 321 10 11734.594         .
      10000  9828 10 7952 "68391610" .  .828125 2113    .05999999865889549 3843 322 11 11734.594         .
      10000  9861 10 7952 "68391610" .  .515625 3215   -.37735849618911743 3843 323 12 11734.594         .
      10000  9891 10 7952 "68391610" .   .40625 1754   -.21212121844291687 3893 324  1 11734.594         .
      10000  9919 10 7952 "68391610" .   .40625  392                     0 3893 325  2 11734.594         .
      10000  9951 10 7952 "68391610" .      .25 1111   -.38461539149284363 3893 326  3 11734.594         .
      10000  9981 10 7952 "68391610" .  .234375  998                -.0625 3893 327  4 11734.594         .
      10000 10010 10 7952 "68391610" .   .21875  815   -.06666667014360428 3893 328  5 11734.594         .
      10000 10042 10 7952 "68391610" 0        .   67                     . 3893 329  6 11734.594         .
      10001  9496  . 7953 "36720410" .        .    .                     .    . 311 12 11734.594 11734.594
      10001  9527 11 7953 "36720410" .    6.125  667                     .  985 312  1 11734.594 11734.594
      10001  9555 11 7953 "36720410" .     6.25 1067   .020408162847161293  985 313  2 11734.594 11734.594
      10001  9586 11 7953 "36720410" .   6.3125  335   .025200003758072853  985 314  3 11734.594 11734.594
      10001  9616 11 7953 "36720410" .    6.375  225   .009900989942252636  985 315  4 11734.594 11734.594
      10001  9646 11 7953 "36720410" .   6.3125  217  -.009803921915590763  985 316  5 11734.594 11734.594
      10001  9677 11 7953 "36720410" .    6.125  238  -.013069307431578636  985 317  6  6033.125 11734.594
      10001  9708 11 7953 "36720410" .   6.0625  429  -.010204081423580647  985 318  7  6033.125 11734.594
      10001  9737 11 7953 "36720410" .      6.5  895    .07216494530439377  985 319  8  6033.125 11734.594
      10001  9769 11 7953 "36720410" .    6.375  366  -.003076923545449972  991 320  9  6033.125 11734.594
      10001  9800 11 7953 "36720410" .    6.625  362    .03921568766236305  991 321 10  6033.125 11734.594
      10001  9828 11 7953 "36720410" .        7  312   .056603774428367615  991 322 11  6033.125 11734.594
      10001  9861 11 7953 "36720410" .        7  312   .014999999664723873  991 323 12  6033.125 11734.594
      10001  9891 11 7953 "36720410" .     6.75  399    -.0357142873108387  991 324  1  6033.125 11734.594
      10001  9919 11 7953 "36720410" .     6.25  365   -.07407407462596893  991 325  2  6033.125 11734.594
      10001  9951 11 7953 "36720410" .    6.375  216    .03680000081658363  991 326  3  6033.125 11734.594
      10001  9981 11 7953 "36720410" .    6.125  188   -.03921568766236305  991 327  4  6033.125 11734.594
      10001 10010 11 7953 "36720410" .   5.6875  211    -.0714285746216774  991 328  5  6033.125 11734.594
      10001 10042 11 7953 "36720410" .    5.875  146   .051428571343421936  991 329  6  5822.125 11734.594
      10001 10073 11 7953 "36720410" .        6  215   .021276595070958138  991 330  7  5822.125  6033.125
      10001 10104 11 7953 "36720410" .      6.5  314     .0833333358168602  991 331  8  5822.125  6033.125
      10001 10134 11 7953 "36720410" .     6.25  461   -.02230769209563732  992 332  9  5822.125  6033.125
      10001 10164 11 7953 "36720410" .    6.375  550   .019999999552965164  992 333 10  5822.125  6033.125
      10001 10195 11 7953 "36720410" .   6.1875  822  -.029411764815449715  992 334 11  5822.125  6033.125
      10001 10226 11 7953 "36720410" .    5.875  960   -.03353535383939743  992 335 12  5822.125  6033.125
      10001 10255 11 7953 "36720410" .     6.25  490    .06382978707551956  992 336  1  5822.125  6033.125
      10001 10286 11 7953 "36720410" .     6.75  382    .07999999821186066  992 337  2  5822.125  6033.125
      10001 10317 11 7953 "36720410" .    6.125  369   -.07629629969596863  992 338  3  5822.125  6033.125
      10001 10346 11 7953 "36720410" .   6.3125  121   .030612245202064514  992 339  4  5822.125  6033.125
      10001 10378 11 7953 "36720410" .   6.4375  102   .019801979884505272  992 340  5  5822.125  6033.125
      10001 10408 11 7953 "36720410" .     6.25  153   -.01203883532434702  992 341  6      6200  6033.125
      10001 10437 11 7953 "36720410" .   6.4375  110   .029999999329447746  992 342  7      6200  5822.125
      10001 10470 11 7953 "36720410" .    6.625   65   .029126213863492012  992 343  8      6200  5822.125
      10001 10500 11 7953 "36720410" .    6.375  119  -.021132076159119606  998 344  9      6200  5822.125
      10001 10531 11 7953 "36720410" .    6.625  212    .03921568766236305  998 345 10      6200  5822.125
      10001 10561 11 7953 "36720410" .    6.625  150                     0  998 346 11      6200  5822.125
      10001 10591 11 7953 "36720410" .    6.375  108  -.021132076159119606  998 347 12      6200  5822.125
      10001 10623 11 7953 "36720410" .      6.5  252   .019607843831181526  998 348  1      6200  5822.125
      10001 10651 11 7953 "36720410" .     6.75  417    .03846153989434242  998 349  2      6200  5822.125
      10001 10682 11 7953 "36720410" .     6.75  241   .017777778208255768  998 350  3      6200  5822.125
      10001 10710 11 7953 "36720410" .     7.25  310    .07407407462596893  998 351  4      6200  5822.125
      10001 10743 11 7953 "36720410" .        7  629   -.03448275849223137  998 352  5      6200  5822.125
      10001 10773 11 7953 "36720410" .        7  200   .017142856493592262 1001 353  6      7007  5822.125
      10001 10804 11 7953 "36720410" .     7.25  185     .0357142873108387 1001 354  7      7007      6200
      10001 10835 11 7953 "36720410" .     9.25  836    .27586206793785095 1001 355  8      7007      6200
      10001 10864 11 7953 "36720410" .    8.875  497  -.027027027681469917 1019 356  9      7007      6200
      10001 10896 11 7953 "36720410" .      9.5 2250    .07042253762483597 1019 357 10      7007      6200
      10001 10926 11 7953 "36720410" .    9.875  411    .03947368264198303 1019 358 11      7007      6200
      10001 10955 11 7953 "36720410" .   10.125  178   .037974681705236435 1022 359 12      7007      6200
      10001 10988 11 7953 "36720410" .   9.9375  353  -.018518518656492233 1022 360  1      7007      6200
      10001 11016 11 7953 "36720410" .    9.875  149  -.006289307959377766 1022 361  2      7007      6200
      10001 11046 11 7953 "36720410" .    9.875  127   .012658228166401386 1027 362  3      7007      6200
      10001 11077 11 7953 "36720410" .    9.875  166                     0 1027 363  4      7007      6200
      10001 11108 11 7953 "36720410" .     9.75  279  -.012658228166401386 1027 364  5      7007      6200
      10001 11137 11 7953 "36720410" .     9.75  105   .014102564193308353 1031 365  6  10052.25      6200
      10001 11169 11 7953 "36720410" .       10  194   .025641025975346565 1031 366  7  10052.25      7007
      10001 11200 11 7953 "36720410" .      9.5  111   -.05000000074505806 1031 367  8  10052.25      7007
      10001 11228 11 7953 "36720410" .     9.75  123    .04078947380185127 1044 368  9  10052.25      7007
      10001 11261 11 7953 "36720410" .    9.625   64  -.012820512987673283 1044 369 10  10052.25      7007
      10001 11291 11 7953 "36720410" .    9.625  206                     0 1044 370 11  10052.25      7007
      10001 11322 11 7953 "36720410" .      9.5  106    .00129870162345469 1054 371 12  10052.25      7007
      10001 11353 11 7953 "36720410" .    9.625  384    .01315789483487606 1054 372  1  10052.25      7007
      10001 11381 11 7953 "36720410" .     9.75  245   .012987012974917889 1054 373  2  10052.25      7007
      10001 11409 11 7953 "36720410" .      9.5  124  -.011538460850715637 1054 374  3  10052.25      7007
      10001 11442 11 7953 "36720410" .    9.875  243    .03947368264198303 1054 375  4  10052.25      7007
      10001 11473 11 7953 "36720410" .    9.875  236                     0 1054 376  5  10052.25      7007
      10001 11501 11 7953 "36720410" .     10.5  255    .07848101109266281 1073 377  6   11266.5      7007
      10001 11534 11 7953 "36720410" .   10.125  161    -.0357142873108387 1073 378  7   11266.5  10052.25
      10001 11564 11 7953 "36720410" .     11.5  654     .1358024626970291 1073 379  8   11266.5  10052.25
      10001 11595 11 7953 "36720410" .     11.5  542   .013043479062616825 1073 380  9   11266.5  10052.25
      10001 11626 11 7953 "36720410" .       13  366     .1304347813129425 1073 381 10   11266.5  10052.25
      10001 11655 11 7953 "36720410" .    14.75  367    .13461539149284363 1073 382 11   11266.5  10052.25
      10001 11687 11 7953 "36720410" .     14.5  269 -.0067796604707837105 1075 383 12   11266.5  10052.25
      10001 11718 11 7953 "36720410" .    13.75  211    -.0517241396009922 1075 384  1   11266.5  10052.25
      10001 11746 11 7953 "36720410" .       11  405   -.20000000298023224 1075 385  2   11266.5  10052.25
      10001 11778 11 7953 "36720410" .    11.75  282    .08181818574666977 1075 386  3   11266.5  10052.25
      10001 11808 11 7953 "36720410" .   11.875  119   .010638297535479069 1075 387  4   11266.5  10052.25
      10001 11837 11 7953 "36720410" .       12  137   .010526316240429878 1075 388  5   11266.5  10052.25
      10001 11869 11 7953 "36720410" .    11.75  204  -.007708333432674408 1075 389  6  12631.25  10052.25
      10001 11900 11 7953 "36720410" .     12.5   59    .06382978707551956 1075 390  7  12631.25   11266.5
      10001 11931 11 7953 "36720410" .       13  157    .03999999910593033 1075 391  8  12631.25   11266.5
      end
      format %d date
      format %tm monthdt

      Regards,

      Thom

      Comment


      • #4
        What are negative share prices (in #1) that require:

        Code:
        replace prc = abs(prc)
        Last edited by Robert Picard; 13 May 2016, 12:18.

        Comment


        • #5
          To solve the issue of values going outside a given firm, you can restrict it by using the bys prefix, see
          Code:
          bys permno ( monthdt) : replace maeq = maeq[_n-1] if missing(maeq)
          Regards
          --------------------------------------------------
          Attaullah Shah, PhD.
          Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
          FinTechProfessor.com
          https://asdocx.com
          Check out my asdoc program, which sends outputs to MS Word.
          For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

          Comment

          Working...
          X