Announcement

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

  • #61
    Dear Clyde,

    Awaiting reply to # 60 post. It would have been very helpful if these modifications are inserted in the given code.

    Adding more to it, finally for all new variables, i.e., CV1-CV5 and CV_a and CE1-CE5 and CE_a (total 12 variables series) i want to run regression with only constant and no other independent variable. The t-stats should be calculated after newey and west (1987) adjustment with number of lags = 6

    Eventually, I require output as both variable series and the regression results (constant value and its t-stat).

    Best Regards.
    Last edited by Sartaj Hussain; 15 Oct 2021, 01:32.

    Comment


    • #62
      I never saw #60. I was on vacation then and not using the internet for a few weeks.

      Anyway, what you're asking for there is not very different from things that have been done earlier in this thread. I think you can do this yourself, using some earlier posts in this thread as a model. At least you should give it a good try. If you don't get it right, post back showing the code you tried and explain, unless it is blatantly obvious, in what way it did not do what you need.

      Comment


      • #63
        Well, depending on my understanding i could write the below code, but obviously it is too lengthy. I am sure it will give you idea what is to be done to write a precise code.

        Code:
         * For value weighted 
         
        gen control_1 = vw_mean_size_q1_idiovol_q5_rt- vw_mean_size_q1_idiovol_q1_rt
        
        gen control_2 = vw_mean_size_q2_idiovol_q5_rt- vw_mean_size_q2_idiovol_q1_rt
        
        gen control_3 = vw_mean_size_q3_idiovol_q5_rt- vw_mean_size_q3_idiovol_q1_rt
        
        gen control_4 = vw_mean_size_q4_idiovol_q5_rt- vw_mean_size_q4_idiovol_q1_rt
        
        gen control_5 = vw_mean_size_q5_idiovol_q5_rt- vw_mean_size_q5_idiovol_q1_rt
        
        gen control_avg = (control_1+control_2+control_3+control_4+control_5)/5
        
        * For equal weighted 
        
        gen control_1 = ew_mean_size_q1_idiovol_q5_rt- ew_mean_size_q1_idiovol_q1_rt
        
        gen control_2 = ew_mean_size_q2_idiovol_q5_rt- ew_mean_size_q2_idiovol_q1_rt
        
        gen control_3 = ew_mean_size_q3_idiovol_q5_rt- ew_mean_size_q3_idiovol_q1_rt
        
        gen control_4 = ew_mean_size_q4_idiovol_q5_rt- ew_mean_size_q4_idiovol_q1_rt
        
        gen control_5 = ew_mean_size_q5_idiovol_q5_rt- ew_mean_size_q5_idiovol_q1_rt
        
        gen control_avg = (control_1+control_2+control_3+control_4+control_5)/5
        Then for newey and west based regressions, i know this code, but it can't be used to run individual regressions for a set of variables in one go. I have to run it number of times there are (y) variables. Well, i write it though.

        Code:
        gen time = _n
        tsset time
        newey control_1, lag(5)
        )

        Hope from this you can converge things into a useful code.
        Last edited by Sartaj Hussain; 15 Oct 2021, 13:12.

        Comment


        • #64
          Code:
          forvalues i = 1/5 {
              gen control_`i' = vw_mean_size_q`i'_idiovol_q5_rt- vw_mean_size_q`i'_idiovol_q1_rt
          }
          egen control_avg = rowmean(control_*)
          will more compactly handle the value-weighted case. For the equally weighted case, the code is similar, and I'm sure you can make the appropriate modifications to this. Just remember that you will need to either -drop- all the control_* variables from the value-weighted case before going to the equally weighted case, or you will need to use different variable names for the equally weighted variables.

          With the understanding that I do not use or know much about the -newey- command, assuming your one -newey- command is correct, you can run it over the whole set of control_* variables with a loop like this:
          Code:
          foreach v of varlist control_* {
              newey `v', lag(5)
          }
          Do read the -foreach- and -forvalues- chapters in the [P] volume of the PDF manuals that come with your Stata installation. The ability to code loops is essential for the kind of work you are involved in. It won't take you long to read those chapters and learn how it is done. The time you invest in that will be quickly and amply repaid by not having to post problems of this nature and then wait for somebody to respond to you. Looping is "bread and butter" Stata, and as you are clearly involved in regular use of Stata beyond the beginner level, it behooves you to acquire this skill.

          Comment


          • #65
            Dear Clyde,

            ​​​​​Thanks for above reply. It was really helpful.

            I run below code on a data set that has observations with missing data on some variable, but i face following error when last line of the code (highlighted in bold) is executed. However, error doesn't come when such observations are removed from data set. This error is arising probably there are missing observations in some quintile groups. I want to run the code in presence of observations with missing data on some variable in the data set. It requires some modification that i do not get.

            ERROR
            size_q._idiovol_q._stock_count invalid variable name
            r(198);



            Code:
            capture program drop one_mdate
            program define one_mdate
                gen cutoffs = .
                _pctile size, nq(5)
                forvalues i = 1/4 {
                    replace cutoffs = r(r`i') in `i'
                }
                display `"`cutoffs'"'
                xtile size_quintile = size, cutpoints(cutoffs)
                drop cutoffs
                by size_quintile, sort: egen idiovol_quintile = xtile(idiovol), nq(5)
                tab *_quintile
                exit
            end
            
            
            // DOUBLE SORT EACH FISCAL YEAR
            runby one_mdate, by(mdate) status
            
            capture program drop one_weighted_return
            program define one_weighted_return
                egen numerator = total(mcap*rt)
                egen denominator = total(mcap)
                gen vw_mean_rt = numerator/denominator
                exit
            end
            
            runby one_weighted_return, by(mdate size_quintile idiovol_quintile)
            
            gen int portfolio_num = (size_quintile-1)*5 + idiovol_quintile
            
            by mdate size_quintile idiovol_quintile, sort: egen ew_mean_rt = mean(rt)
            
            keep mdate *_quintile *_mean_rt
            by mdate *_quintile, sort: gen stock_count = _N
            by mdate *_quintile, sort: keep if _n == 1
            
            gen str32 group = "size_q" + string(size_quintile) +"_idiovol_q" + string(idiovol_q) + "_"
            drop *_quintile
            reshape wide @stock_count vw_mean_@rt ew_mean_@rt, i(mdate) j(group) string
            Last edited by Sartaj Hussain; 17 Oct 2021, 08:34.

            Comment


            • #66
              It is not hard to code around this to prevent this error message: The last paragraph becomes:

              Code:
              mvencode size_quintile idiovol_q, mv(99)
              gen str32 group = "size_q" + string(size_quintile) +"_idiovol_q" + string(idiovol_q) + "_"
              drop *_quintile
              reshape wide @stock_count vw_mean_@rt ew_mean_@rt, i(mdate) j(group) string
              But a result of that is you will now have additional variables like size_q99_idiovol_q99_stock_count, where 99 expresses the fact that size_quintile or idiovol_q had missing values. It isn't clear to me how missing values arise there in the first place, other than through missing values for size or idiovol. So these variables with 99 in their names are statistics calculated on a portfolio consisting of a hodge-podge of observations where the key variables defining portfolios in your analysis were missing. Perhaps that somehow makes sense in the context of what you are doing, but it seems to me that these will not be useful and that it would be better to drop such observations in the first place.

              Comment


              • #67
                but it seems to me that these will not be useful and that it would be better to drop such observations in the first place.
                Actually that is what i want. But my data is in cross-sectional format. If an observation has missing value on one variable, dropping that missing variable before running the code would remove the entire observation, hence losing other variables also that are not missing.

                This issue needs to be taken care within the above code.Therefore, in the entire process of portfolio construction (which involves two stages: bi-variate sort and construction of portfolio), I want that if there are any observations with missing value, they should simply be ignored or dropped case by case.

                Hope that makes sense and can be incorporated into the above code by your support.

                Comment


                • #68
                  Well, I have no idea what "case by case" means here. I can see two modifications to the code, but I'm not sure which, if either, will do what you want.

                  First approach:
                  Code:
                  mvencode size_quintile idiovol_q, mv(99)
                  gen str32 group = "size_q" + string(size_quintile) +"_idiovol_q" + string(idiovol_q) + "_"
                  drop *_quintile
                  reshape wide @stock_count vw_mean_@rt ew_mean_@rt, i(mdate) j(group) string
                  drop *q99*
                  This retains all of the information in the other variables and simply eliminates the stock count, vw_mean_rt and ew_mean_rt varibles that are associated with these meaningless (q99) portfolios.

                  Second approach:
                  Code:
                  drop if inlist(., size_quintile, idiovol_q)
                  gen str32 group = "size_q" + string(size_quintile) +"_idiovol_q" + string(idiovol_q) + "_"
                  drop *_quintile
                  reshape wide @stock_count vw_mean_@rt ew_mean_@rt, i(mdate) j(group) string
                  This drops all observations that belong to meaningless portfolios--and all of the other variables about them are also lost. I suspect you don't want this, unless this process is occurring sufficiently late in the process that those other variables are no longer needed anyway.

                  Comment


                  • #69
                    Well, I have no idea what "case by case" means here.
                    By this i mean to say: During quintile formation, each return observation is sorted relative to a given sort variable. Similarly for vw portfolio construction, each return observation is weighted by its mcap value. In these two processes, i mean if there is any case which has missing data problem should be ignored or dropped. Now, missing data problem can be either with return or sort variable (size and idiovol in the given case) and also mcap variable. That was my point. Hope that make sense.

                    P.S. I ran both #68, (code #1 and code#2), results are very similar.
                    Last edited by Sartaj Hussain; 17 Oct 2021, 12:28.

                    Comment


                    • #70
                      Added to #69 post, i want to mention that in one of our previous codes we have put these two conditions.

                      Code:
                      if !missing(size_quintile,idiovol_quintile)
                      {
                      in program one_weighted_return part of code and then:

                      Code:
                      drop if missing(size_quintile,idiovol_quintile, vw_mean_rt, ew_mean_rt)
                      after this line of the main code:

                      Code:
                      by mdate size_quintile idiovol_quintile, sort: egen ew_mean_rt = mean(rt)
                      Are these addition not going to do what is intended in #69?

                      I tried using code in #65 after these modifications, it ran but with following error:

                      runby one_weighted_return, by(mdate size_quintile idiovol_quintile)

                      Number of by-groups = 5,755
                      by-groups with errors = 655
                      by-groups with no data = 0
                      Observations processed = 113,256
                      Observations saved = 110,050

                      .
                      end of do-file

                      Please favor with your valuable opinion.
                      Last edited by Sartaj Hussain; 17 Oct 2021, 23:46.

                      Comment


                      • #71
                        I tried using code in #65 after these modifications, it ran but with following error:
                        Please post back with a fresh -dataex- that reproduces this error and also post the entire sequence of code leading from that data up to and including this error, and I will troubleshoot.

                        As for the other questions relating to the missing values, those commands will remove the offending observations at the point where they apply, but it may be that other code is reintroducing such observations. I don't know what modifications you are making to the code posted here, nor what other code has been run before the snippets shown here or between them. So I really can't say just why we are encountering this. It is clear, however, from #65, that observations with missing values for size_quintile and idiovol_quintile are present at the time that code is run. We can try to figure out why and deal with them, but to do that, I need a good -dataex- example and the complete code in use.

                        Comment


                        • #72
                          Code:
                          * Example generated by -dataex-. To install: ssc install dataex
                          clear
                          input int s_id str51 stock float(mdate pr rt mcap idiovol beta size bmratio mom rev illiq coskew idioskew)
                          1 "3I Infotech Ltd."           582   107.9  .0838774  1300.66 .0791094  .8957983 7.170627 .4481165 -.12633932 -.1669456 .0026881 -1.4033834  1.2621658
                          1 "3I Infotech Ltd."           583   112.6  .0435588  1410.03 .1176593  .8941596 7.251366 .4133457  -.2648224  .0838774 .0038134 -1.3507864  1.3801864
                          1 "3I Infotech Ltd."           584    68.7 -.3898757  1471.64 .0692917  .9035137 7.294133 .3960924 -.17417905  .0435588 .0017423 -1.4160223  1.3780096
                          1 "3I Infotech Ltd."           585   41.95  -.389374   898.22 .0772024  .9303561 6.800415 .7110626  -.1374322 -.3898757 .0049241 -1.9217837  1.2305558
                          1 "3I Infotech Ltd."           586    32.4 -.2276519    548.5 .1910248 1.0543237 6.307187 1.164482  -.4467936  -.389374  .015475  -1.540114  1.0260595
                          1 "3I Infotech Ltd."           587   38.65  .1929012   423.63 .0835618 1.0544907 6.048861 1.507716  -.8273377 -.2276519 .0103564 -1.5301414    .903521
                          1 "3I Infotech Ltd."           588   35.65 -.0776197   505.35 .1445049 1.1047881 6.225251 1.317464 -1.1370238  .1929012 .0095969  -1.991793   .9232385
                          1 "3I Infotech Ltd."           589   28.95 -.1879383   466.13 .1259066 1.1740319 6.144464 1.428331  -.7739682 -.0776197 .0039669 -1.5736206    .662843
                          1 "3I Infotech Ltd."           590    32.5  .1226252   378.52 .0846883  1.198705 5.936269 1.758895  -.8983014 -.1879383 .0020465 -1.3750672   .7003378
                          1 "3I Infotech Ltd."           591   45.95  .4138462   424.94 .0701886 1.1770436 6.051948 1.521846  -.8540121  .1226252 .0046898 -2.2867496   .7316203
                          1 "3I Infotech Ltd."           592   73.45  .5984765    600.8  .194191 1.1731851 6.398262 1.076387 -1.0945559  .4138462 .0009951 -2.5983424   1.090149
                          1 "3I Infotech Ltd."           593    71.6 -.0251872   960.37 .2080131 1.2537544 6.867319 .6733833 -.58259636  .5984765 .0070034  -1.696038  1.2895592
                          2 "3M India Ltd."              582    1605 -.0403587  1884.08 .0945285  .5504192 7.541195 .1718505 -.04965548 -.0961414 .0324018    .663267   .8722552
                          2 "3M India Ltd."              583 1624.75  .0123053  1808.04 .0789487 .56862265 7.499999 .1790779 -.00929656 -.0403587 .1150158   .4075693   .8189214
                          2 "3M India Ltd."              584  1439.9 -.1137713  1830.29 .0702951   .555436  7.51223 .1769011  -.0061362  .0123053 .0659989  .18330938   .8100186
                          2 "3M India Ltd."              585  930.35 -.3538788  1622.06 .0687143 .55424374 7.391452 .2075352 -.03710564 -.1137713 .0524532 -.17885327    .807752
                          2 "3M India Ltd."              586  916.05 -.0153705  1048.05 .1174414  .4366922 6.954687 .3212017  -.3864366 -.3538788 .1614821 -1.2584523  .15513664
                          2 "3M India Ltd."              587     926  .0108619  1031.94        .  .4371958 6.939196 .3262158  -.6253247 -.0153705 .2712789  -.7098808   .1790273
                          2 "3M India Ltd."              588   945.5  .0210583  1043.15 .0563063  .4049516     6.95 .3333693  -.8057244  .0108619 .2083724  -.8741335  .03139757
                          2 "3M India Ltd."              589  893.85 -.0546272  1065.11 .0590803 .33627075 6.970833 .3264939  -.6232147  .0210583 .1946547 -.08674618  .21036015
                          2 "3M India Ltd."              590     879 -.0166135  1006.93        .  .3306707 6.914661   .34536  -.6206292 -.0546272 1.510476 -.17237993  .01673686
                          2 "3M India Ltd."              591  1024.6  .1656428    990.2 .1408168  .3201537 6.897907 .3639932  -.6131397 -.0166135 .1730662  -.2353555   .1885965
                          2 "3M India Ltd."              592  1241.3  .2114972  1154.22 .1939789   .371392  7.05118 .3122682  -.6534325  .1656428 .2436674  -.8175713   .9254652
                          2 "3M India Ltd."              593    1285   .035205  1398.33 .1210847  .3899597 7.243034  .257754 -.48089325  .2114972 .0481781 -1.0205089   .9289688
                          3 "63 Moons Technologies Ltd." 582 1441.15  -.142147  7708.22 .1038105 1.0037644 8.950043 .2126611  -.4747278 -.0323705  .001202  -2.361518    .836089
                          3 "63 Moons Technologies Ltd." 583    1424 -.0119002  6612.52 .1323558  .9775379 8.796721 .2478992 -.34828845  -.142147 .0009891 -2.3182673   .6366838
                          3 "63 Moons Technologies Ltd." 584 1034.75 -.2733497  6533.83 .0728888  .9427468 8.784749 .2508848  -.4611148 -.0119002 .0007456 -2.0230758   .4945134
                          3 "63 Moons Technologies Ltd." 585  510.95 -.5062092  4747.81    .1093  .9315034 8.465439 .3614883  -.5940032 -.2733497 .0007692  -2.712284  .24077667
                          3 "63 Moons Technologies Ltd." 586   587.6  .1500146  2344.42 .1548035 1.0700614 7.759793 .7320677  -.8046051 -.5062092 .0047458  -1.584189 -.22960605
                          3 "63 Moons Technologies Ltd." 587     499 -.1507828  2696.12 .1724489 1.0461704 7.899569 .6365725 -1.2564824  .1500146 .0023662  -.3913108  .05654725
                          3 "63 Moons Technologies Ltd." 588   478.7 -.0406813  2289.59 .1338877 1.0402232 7.736128  .768477 -1.1768951 -.1507828 .0012496 -.19957256  .05539269
                          3 "63 Moons Technologies Ltd." 589   423.6 -.1151034  2196.45  .068043 1.0022038 7.694598 .8010654 -1.1431617 -.0406813 .0016374 -.11193354  .04199263
                          3 "63 Moons Technologies Ltd." 590   619.9  .4634089  1943.63 .1194559 1.0311736 7.572313 .9052644 -1.1361648 -.1151034 .0022114   .1376895   .1781995
                          3 "63 Moons Technologies Ltd." 591  707.65  .1415551  2844.33 .1092815  .9769729 7.953083 .6151475 -1.0393578  .4634089 .0014219   .8105374   .1951375
                          3 "63 Moons Technologies Ltd." 592  1446.5   1.04409  3246.96 .0868962  .9995847 8.085474 .5388681  -.6623359  .1415551 .0002822   .7207437   .1832574
                          3 "63 Moons Technologies Ltd." 593  1307.3 -.0962323  6637.07 .2136188 1.0656089 8.800426 .2636225 -.51756567   1.04409 .0038525   1.535526   .8561664
                          4 "A B B India Ltd."           582   770.1 -.0510167 17196.36 .0744674   .832959 9.752453  .108244  .01307075 -.2075195 .0003755  -.5570521   .4166189
                          4 "A B B India Ltd."           583   878.5   .140761 16319.06 .1217215  .8416988 9.700089 .1140631 -.24201564 -.0510167 .0005074  -.3640922  .07702978
                          4 "A B B India Ltd."           584   784.9 -.1065452 18616.15   .07044  .8088375 9.831785 .0999886  -.2728126   .140761 .0002135   -.349309  .04015106
                          4 "A B B India Ltd."           585  535.65 -.3175564 16632.69 .0592615  .8106629 9.719126 .1182061 -.28264767 -.1065452 .0003473  -.4228735  .04250785
                          4 "A B B India Ltd."           586   437.9 -.1824886 11350.87 .1467664  .8811383 9.337049 .1732101  -.6069741 -.3175564 .0012176 -2.4606845  -.3731064
                          4 "A B B India Ltd."           587  453.45  .0355104  9279.47 .0978662  .9122386  9.13556 .2118749  -.9133573 -.1824886 .0006658 -2.2701082  -.2783756
                          4 "A B B India Ltd."           588   479.8    .05811  9608.99 .0655202  .9129699 9.170454 .2190319 -1.0667948  .0355104 .0005836  -2.311827  -.3233492
                          4 "A B B India Ltd."           589  367.65 -.2337432 10167.36 .1042047  .9118398 9.226938 .2070029  -.7721182    .05811 .0008996  -2.411128  -.3627294
                          4 "A B B India Ltd."           590   427.4  .1625187  7790.81 .0955599  .9314957   8.9607 .2701482   -.745961 -.2337432 .0008895 -2.1981113 -.36798945
                          4 "A B B India Ltd."           591  487.35  .1402668  9056.96 .0934217  .9214678 9.111289 .2410388  -.9950995  .1625187 .0003751 -2.5660474   -.388953
                          4 "A B B India Ltd."           592   649.7  .3331282 10327.35 .1215041  .9258978 9.242551 .2113881  -.8049828  .1402668 .0004761  -2.775156 -.19926205
                          4 "A B B India Ltd."           593   777.1  .1960904 13767.69 .1822933  .9528525  9.53008 .1585655  -.5617028  .3331282 .0038507 -2.5738716  .06192054
                          5 "A B G Shipyard Ltd."        582   336.8  .0023809  1710.97 .1396551  .7913384 7.444816 .4278274   .4182359 -.3892019 .0063787 -3.3355446   .5100719
                          5 "A B G Shipyard Ltd."        583  362.45   .076158  1715.05 .1610839  .7561606 7.447197 .4268112  -.3066918  .0023809 .0066063 -1.9838413   .4239286
                          5 "A B G Shipyard Ltd."        584   298.6 -.1761622  1845.66 .0738923  .7791618 7.520592 .3966064  -.2124559   .076158 .0022315  -2.538337   .5012522
                          5 "A B G Shipyard Ltd."        585   115.3 -.6138647  1520.52 .0868495  .7594597 7.326807 .4985934  -.3513617 -.1761622 .0047463  -1.972155   .5859241
                          5 "A B G Shipyard Ltd."        586   97.45 -.1548136   587.13 .2024035   .893261 6.375246  1.29124  -.7107053 -.6138647 .0281276  -3.354473  .09760043
                          5 "A B G Shipyard Ltd."        587   133.4  .3689071   496.23 .0782214  .8622221 6.207039 1.527758 -1.6249357 -.1548136 .0131701  -1.769576  -.0898203
                          5 "A B G Shipyard Ltd."        588    70.1 -.4745127    679.3 .2176897  .8696815 6.521063 1.183808 -1.7767352  .3689071 .0070357  -3.101864   .4325186
                          5 "A B G Shipyard Ltd."        589   83.25  .1875892   356.96 .1819502  .9021762 5.877624 2.252782 -1.1264615 -.4745127  .004963 -4.5782504   .3841798
                          5 "A B G Shipyard Ltd."        590    82.8 -.0054054   423.92 .1701636  .9180753 6.049545 1.896937 -1.5662667  .1875892 .0012445 -4.3800807   .5055168
                          5 "A B G Shipyard Ltd."        591   114.8  .3864734   421.63 .1424798  .9778091 6.044128 1.989251  -1.424076 -.0054054 .0019382  -5.074934    .499775
                          5 "A B G Shipyard Ltd."        592  224.45  .9551393   584.58 .2416571   1.08625 6.370893 1.434756 -1.2780082  .3864734 .0013215  -5.314609   .6503295
                          5 "A B G Shipyard Ltd."        593  198.95  -.113611  1142.94 .2144302 1.1334819 7.041359 .7338383  -.7924519  .9551393 .0020673 -4.0301075   .7002978
                          6 "A C C Ltd."                 582   584.3  .1182775  9804.72 .0888697  .7693109 9.190619 .4876555 -.27696294 -.2091123 .0002736  -2.822503 -.10143326
                          6 "A C C Ltd."                 583  561.65 -.0387643 10964.48 .1286077  .7471054 9.302416 .4360774  -.6211684  .1182775 .0003969  -2.985709  -.1997642
                          6 "A C C Ltd."                 584  611.65  .0890234 10539.54 .0814341  .7269282 9.262889 .4536633  -.5085044 -.0387643 .0002722 -2.7649505 -.20128797
                          6 "A C C Ltd."                 585   493.4 -.1933296  11477.8 .0654252  .6773509  9.34817 .4412654  -.6682932  .0890234 .0001937 -2.5826764 -.29695332
                          6 "A C C Ltd."                 586  406.25 -.1766315  9258.83 .1317637  .6466519 9.133333 .5470207  -.4810188 -.1933296 .0013751  -1.715922  -.3301926
                          6 "A C C Ltd."                 587   477.9  .1763692  7623.52  .125151  .6608026 8.938993 .6643692  -.6854388 -.1766315 .0007359 -1.9595603  -.3994362
                          6 "A C C Ltd."                 588  504.85  .0563926  8969.31 .1007549  .7195417 9.101564 .5493827  -.8024532  .1763692 .0004708  -1.439382  -.4040527
                          6 "A C C Ltd."                 589   539.8  .0692284  9475.17 .0943909  .7197791  9.15643 .5200554  -.3900176  .0563926  .000392 -1.0564758 -.14830434
                          6 "A C C Ltd."                 590  576.65  .0682661 10131.12 .0934666  .7322345 9.223367 .4863838   -.346466  .0692284 .0002354  -.9772217 -.16816477
                          6 "A C C Ltd."                 591     653  .1324026 10822.84 .0653843   .753518 9.289414 .4926905   -.319372  .0682661 .0002462  -.9229993 -.14441592
                          6 "A C C Ltd."                 592     783  .1990812 12255.82  .095833   .752023 9.413756 .4350842  -.1694572  .1324026 .0001953 -1.2742578 -.13297321
                          6 "A C C Ltd."                 593   768.9 -.0180076 14696.14 .1408703  .7617074  9.59534 .3628353  .09212224  .1990812 .0035031 -1.0264001 -.00727474
                          7 "A I A Engineering Ltd."     582   255.6  .0162214  2363.88 .0581261  .7958049  7.76806 .2335957 -.06549466 -.1934066 .0692599  -.6260848   .8918313
                          7 "A I A Engineering Ltd."     583  313.38  .2260563  2402.23 .0893585  .7441835 7.784153  .229867 -.13198939  .0162214 .0952409  -1.226386   .7527254
                          7 "A I A Engineering Ltd."     584  269.97 -.1385219  2945.27 .0650951  .7174511 7.987956 .1874848 -.04077616  .2260563 .0445475 -1.2685347    .753996
                          7 "A I A Engineering Ltd."     585   155.8 -.4228988  2537.28 .0901486  .7119604 7.838848 .2278031  .24033707 -.1385219 .0526978  -1.405125   .5611397
                          7 "A I A Engineering Ltd."     586  124.95 -.1980103  1464.27 .1693883  .6732609 7.289112 .3947368 -.00271862 -.4228988 .1935599  -2.849101 -.31739795
                          7 "A I A Engineering Ltd."     587   136.1  .0892358  1174.33 .1370389  .6822902 7.068453 .4921969  -.4690253 -.1980103 .0406018 -2.1152267  -.4536062
                          7 "A I A Engineering Ltd."     588  114.05 -.1620132  1279.12 .1125702  .6748999 7.153928 .4746509  -.7592906  .0892358 .0435148 -2.0833333 -.21065316
                          7 "A I A Engineering Ltd."     589  116.15   .018413  1071.89 .1387436  .6517614 6.977179 .5664182  -.5515879 -.1620132 .0656963   -2.72293 -.17259285
                          7 "A I A Engineering Ltd."     590  124.45  .0714593  1091.62 .0639747   .653017 6.995418 .5561773  -.8733688   .018413   .07222  -2.419581 -.15481132
                          7 "A I A Engineering Ltd."     591  169.25   .359984  1169.63 .1627489  .6240959 7.064443 .5546002  -.7269815  .0714593  .025908  -3.400641  .10350284
                          7 "A I A Engineering Ltd."     592  241.25  .4254062  1590.68 .1256488   .686071 7.371917 .4077991  -.7309081   .359984 .0088803  -3.116629   .1878451
                          7 "A I A Engineering Ltd."     593  227.25 -.0580311  2267.36 .1219077  .7222152 7.726371 .2860933 -.33348125  .4254062 .0620181  -2.573541  .11304089
                          8 "Aban Offshore Ltd."         582 2502.25 -.1301212 10872.86 .0833856  1.095154 9.294025 .0715927  .46470425 -.2810063 .0001606  -2.289098 -.13397793
                          8 "Aban Offshore Ltd."         583  2080.2 -.1686682  9458.07 .1201344 1.0798002 9.154624 .0823019  .18721692 -.1301212 .0001296 -2.3768287 -.12070557
                          8 "Aban Offshore Ltd."         584 2049.85 -.0145899  7862.79 .0719796 1.0449556 8.969896 .0990001  .08449736 -.1686682 .0001089 -2.0108082   -.094764
                          8 "Aban Offshore Ltd."         585   795.6  -.611874   7748.9 .1200437 1.0192004 8.955306 .1109545  -.2840065 -.0145899 .0000935  -2.341828  -.0935983
                          8 "Aban Offshore Ltd."         586   693.1 -.1288336  3007.55 .2719017  1.256357 8.008881 .2858723  -.6351557  -.611874 .0004018 -1.6526965  -.3275574
                          8 "Aban Offshore Ltd."         587   671.7 -.0308757  2620.08 .1332539 1.2491058  7.87096 .3281489 -1.2225515 -.1288336 .0002849  -.6521291  -.3671528
                          8 "Aban Offshore Ltd."         588   484.2 -.2791425  2539.18 .1546266  1.225027 7.839596 .3606074 -1.4337457 -.0308757  .000188  -.6997693   -.379891
                          8 "Aban Offshore Ltd."         589  315.75 -.3478934  1830.39 .1649709  1.238564 7.512284 .5002478   -1.20281 -.2791425 .0002161   .4037297 -.52581537
                          8 "Aban Offshore Ltd."         590  399.65  .2657165  1193.61 .1096739 1.2438087 7.084738 .7671259  -1.608876 -.3478934 .0001826   .8031088  -.4628556
                          8 "Aban Offshore Ltd."         591  407.35  .0192669  1510.77 .1892143 1.3248554 7.320375 .6084074  -1.692288  .2657165 .0001198  -1.272794 -.04180823
                          8 "Aban Offshore Ltd."         592  905.35  1.222536  1539.88 .1369767 1.3458644  7.33946 .5969068 -1.5530345  .0192669 .0000695  -.6858675 -.04058292
                          8 "Aban Offshore Ltd."         593   894.5 -.0119843  3422.43  .308261  1.420559 8.138106 .2685702 -1.7080214  1.222536 .0009972   .7390614   .6606715
                          9 "Abbott India Ltd."          582   544.8  .0186034   774.07 .0575914  .2559861 6.651662 .3402076  .03810834    .01557 .0117642  -.4820676  1.3073744
                          9 "Abbott India Ltd."          583   560.9  .0295522   745.03 .0666287 .20880777 6.613424 .3181351    .040532  .0186034 .0098854 -1.2728097  1.0129037
                          9 "Abbott India Ltd."          584   485.7 -.1340703   767.04 .0174757 .20387904 6.642539 .3090034 -.03217566  .0295522 .0015339 -1.0881753   .4121031
                          9 "Abbott India Ltd."          585  442.05 -.0898703   664.21 .0479385 .21832784 6.498599 .3833025 -.00843637 -.1340703 .0191308  -1.361761   .3169914
                          end
                          format %tm mdate
                          The code is given below:-

                          Code:
                          capture program drop one_mdate
                          program define one_mdate
                              gen cutoffs = .
                              _pctile mcap, nq(5)
                              forvalues i = 1/4 {
                                  replace cutoffs = r(r`i') in `i'
                              }
                              display `"`cutoffs'"'
                              xtile mcap_quintile = mcap, cutpoints(cutoffs)
                              drop cutoffs
                              by mcap_quintile, sort: egen idiovol_quintile = xtile(idiovol), nq(5)
                              tab *_quintile
                              exit
                          end
                          
                          
                          // DOUBLE SORT EACH FISCAL YEAR
                          runby one_mdate, by(mdate) status
                          
                          capture program drop one_weighted_return
                          program define one_weighted_return
                          if !missing(mcap_quintile,idiovol_quintile){
                              egen numerator = total(mcap*rt)
                              egen denominator = total(mcap)
                              gen vw_mean_rt = numerator/denominator
                              exit
                          end
                          
                          runby one_weighted_return, by(mdate mcap_quintile idiovol_quintile)
                          
                          gen int portfolio_num = (mcap_quintile-1)*5 + idiovol_quintile
                          
                          by mdate mcap_quintile idiovol_quintile, sort: egen ew_mean_rt = mean(rt)
                          drop if missing(mcap_quintile,idiovol_quintile, vw_mean_rt, ew_mean_rt)
                          keep mdate *_quintile *_mean_rt
                          by mdate *_quintile, sort: gen stock_count = _N
                          by mdate *_quintile, sort: keep if _n == 1
                          
                          gen str32 group = "mcap_q" + string(mcap_quintile) +"_idiovol_q" +string(idiovol_q) + "_"
                          drop *_quintile
                          reshape wide @stock_count vw_mean_@rt ew_mean_@rt, i(mdate) j(group) string

                          Comment


                          • #73
                            OK, turns out it was a pretty simple problem. When you inserted the -if !missing(mcap_quintiile, idiovol_quintile) { - command into program one_weighted_return, you did not also insert a closing brace (}) at the end of the block of code that was to be so restricted. The fix is very simple:

                            Code:
                            capture program drop one_weighted_return
                            program define one_weighted_return
                                if !missing(mcap_quintile,idiovol_quintile){
                                    egen numerator = total(mcap*rt)
                                    egen denominator = total(mcap)
                                    gen vw_mean_rt = numerator/denominator
                                }
                                exit
                            end
                            Curiously, this only triggered errors on the caes where mcap_quintile or idiovol_quintile is missing. Apparently, when those were not missing, the code executed, and on finding the exit command didn't care that it had not found a closing brace to end the block of code. But when one of those was missing, it had no choice but to persist in searching for a closing brace to know where to pick up the execution, and, finding none, it balked, and notified you of the problem in -runby-'s output (as well it should).

                            With this change, the code you sent in #72 runs without error messages with the data shown there. And there are no complaints from -reshape- at the end about invalid names that would have arisen had missing values of mcap_quntile or idiovol_q slipped through. So I think we're OK now.

                            Comment


                            • #74
                              Finally, you figured it out. Thanks a lot! Hope the insertion I made in code as per #70 will take care of issues raised in #69.

                              Comment


                              • #75
                                Well, the code you show in #72 incorporates the those insertions. There is no need to insert them again. The first one actually caused the problem because you didn't also insert a closing brace to match. But my code in #73 fixes that. So if you just add that closing brace as shown in #73, you should be fine. And the issues raised in #69 should be gone--at least they don't surface when the code in #73 runs with the data in #73. And it doesn't look like there is any way for them to arise.

                                Comment

                                Working...
                                X