Announcement

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

  • #31
    ow i want to do sorts on annual basis on June month end each year. So, each year, first sort is to be done using recent June month value of sort variable, i.e. (mcap) and the second sort should be done using recent March month value of sort variable, i,e, (mom).
    I don't understand what this means. Can you give an example?

    Comment


    • #32
      What kind of example if you may kindly make it bit clear for me.

      Comment


      • #33
        Show example data for a couple of firms for a couple of years and point out which june and which march months are to be used in each year. Also do you mean a normal calendar year here, or are you referring to some kind of fiscal year that runs July to June or June to May or something like that? I just don't grasp the time points involved.
        Last edited by Clyde Schechter; 10 Aug 2021, 11:42.

        Comment


        • #34
          Ok. I am appending the example data set here. The procedure is explained herein below:

          1. In June of each year y, all the return observations (rt) will be divided into two groups based on the median mcap of June month of year y.
          2. Next in same time period,i.e. June month of each year y, all the return observations (rt) will be independently divided into three groups following given percentiles based on the (mom) value observed in March month of year y.
          3. Next weighted average portfolio returns for 2*3=6 portfolios will be generated from July of year y to June of year y+1.
          4. After this, steps 1,2 and three are repeated for each year in the sample until end.

          Hope that makes the problem clear.

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input float stock_id str51 stock float(mdate rt mcap mom)
          1 "3I Infotech Ltd."           582  .0838774  1300.66 -.12633932
          1 "3I Infotech Ltd."           583  .0435588  1410.03  -.2648224
          1 "3I Infotech Ltd."           584 -.3898757  1471.64 -.17417905
          1 "3I Infotech Ltd."           585  -.389374   898.22  -.1374322
          1 "3I Infotech Ltd."           586 -.2276519    548.5  -.4467936
          1 "3I Infotech Ltd."           587  .1929012   423.63  -.8273377
          1 "3I Infotech Ltd."           588 -.0776197   505.35 -1.1370238
          1 "3I Infotech Ltd."           589 -.1879383   466.13  -.7739682
          1 "3I Infotech Ltd."           590  .1226252   378.52  -.8983014
          1 "3I Infotech Ltd."           591  .4138462   424.94  -.8540121
          1 "3I Infotech Ltd."           592  .5984765    600.8 -1.0945559
          1 "3I Infotech Ltd."           593 -.0251872   960.37 -.58259636
          2 "3M India Ltd."              582 -.0403587  1884.08 -.04965548
          2 "3M India Ltd."              583  .0123053  1808.04 -.00929656
          2 "3M India Ltd."              584 -.1137713  1830.29  -.0061362
          2 "3M India Ltd."              585 -.3538788  1622.06 -.03710564
          2 "3M India Ltd."              586 -.0153705  1048.05  -.3864366
          2 "3M India Ltd."              587  .0108619  1031.94  -.6253247
          2 "3M India Ltd."              588  .0210583  1043.15  -.8057244
          2 "3M India Ltd."              589 -.0546272  1065.11  -.6232147
          2 "3M India Ltd."              590 -.0166135  1006.93  -.6206292
          2 "3M India Ltd."              591  .1656428    990.2  -.6131397
          2 "3M India Ltd."              592  .2114972  1154.22  -.6534325
          2 "3M India Ltd."              593   .035205  1398.33 -.48089325
          3 "63 Moons Technologies Ltd." 582  -.142147  7708.22  -.4747278
          3 "63 Moons Technologies Ltd." 583 -.0119002  6612.52 -.34828845
          3 "63 Moons Technologies Ltd." 584 -.2733497  6533.83  -.4611148
          3 "63 Moons Technologies Ltd." 585 -.5062092  4747.81  -.5940032
          3 "63 Moons Technologies Ltd." 586  .1500146  2344.42  -.8046051
          3 "63 Moons Technologies Ltd." 587 -.1507828  2696.12 -1.2564824
          3 "63 Moons Technologies Ltd." 588 -.0406813  2289.59 -1.1768951
          3 "63 Moons Technologies Ltd." 589 -.1151034  2196.45 -1.1431617
          3 "63 Moons Technologies Ltd." 590  .4634089  1943.63 -1.1361648
          3 "63 Moons Technologies Ltd." 591  .1415551  2844.33 -1.0393578
          3 "63 Moons Technologies Ltd." 592   1.04409  3246.96  -.6623359
          3 "63 Moons Technologies Ltd." 593 -.0962323  6637.07 -.51756567
          4 "A B B India Ltd."           582 -.0510167 17196.36  .01307075
          4 "A B B India Ltd."           583   .140761 16319.06 -.24201564
          4 "A B B India Ltd."           584 -.1065452 18616.15  -.2728126
          4 "A B B India Ltd."           585 -.3175564 16632.69 -.28264767
          4 "A B B India Ltd."           586 -.1824886 11350.87  -.6069741
          4 "A B B India Ltd."           587  .0355104  9279.47  -.9133573
          4 "A B B India Ltd."           588    .05811  9608.99 -1.0667948
          4 "A B B India Ltd."           589 -.2337432 10167.36  -.7721182
          4 "A B B India Ltd."           590  .1625187  7790.81   -.745961
          4 "A B B India Ltd."           591  .1402668  9056.96  -.9950995
          4 "A B B India Ltd."           592  .3331282 10327.35  -.8049828
          4 "A B B India Ltd."           593  .1960904 13767.69  -.5617028
          5 "A B G Shipyard Ltd."        582  .0023809  1710.97   .4182359
          5 "A B G Shipyard Ltd."        583   .076158  1715.05  -.3066918
          5 "A B G Shipyard Ltd."        584 -.1761622  1845.66  -.2124559
          5 "A B G Shipyard Ltd."        585 -.6138647  1520.52  -.3513617
          5 "A B G Shipyard Ltd."        586 -.1548136   587.13  -.7107053
          5 "A B G Shipyard Ltd."        587  .3689071   496.23 -1.6249357
          5 "A B G Shipyard Ltd."        588 -.4745127    679.3 -1.7767352
          5 "A B G Shipyard Ltd."        589  .1875892   356.96 -1.1264615
          5 "A B G Shipyard Ltd."        590 -.0054054   423.92 -1.5662667
          5 "A B G Shipyard Ltd."        591  .3864734   421.63  -1.424076
          5 "A B G Shipyard Ltd."        592  .9551393   584.58 -1.2780082
          5 "A B G Shipyard Ltd."        593  -.113611  1142.94  -.7924519
          6 "A C C Ltd."                 582  .1182775  9804.72 -.27696294
          6 "A C C Ltd."                 583 -.0387643 10964.48  -.6211684
          6 "A C C Ltd."                 584  .0890234 10539.54  -.5085044
          6 "A C C Ltd."                 585 -.1933296  11477.8  -.6682932
          6 "A C C Ltd."                 586 -.1766315  9258.83  -.4810188
          6 "A C C Ltd."                 587  .1763692  7623.52  -.6854388
          6 "A C C Ltd."                 588  .0563926  8969.31  -.8024532
          6 "A C C Ltd."                 589  .0692284  9475.17  -.3900176
          6 "A C C Ltd."                 590  .0682661 10131.12   -.346466
          6 "A C C Ltd."                 591  .1324026 10822.84   -.319372
          6 "A C C Ltd."                 592  .1990812 12255.82  -.1694572
          6 "A C C Ltd."                 593 -.0180076 14696.14  .09212224
          7 "A I A Engineering Ltd."     582  .0162214  2363.88 -.06549466
          7 "A I A Engineering Ltd."     583  .2260563  2402.23 -.13198939
          7 "A I A Engineering Ltd."     584 -.1385219  2945.27 -.04077616
          7 "A I A Engineering Ltd."     585 -.4228988  2537.28  .24033707
          7 "A I A Engineering Ltd."     586 -.1980103  1464.27 -.00271862
          7 "A I A Engineering Ltd."     587  .0892358  1174.33  -.4690253
          7 "A I A Engineering Ltd."     588 -.1620132  1279.12  -.7592906
          7 "A I A Engineering Ltd."     589   .018413  1071.89  -.5515879
          7 "A I A Engineering Ltd."     590  .0714593  1091.62  -.8733688
          7 "A I A Engineering Ltd."     591   .359984  1169.63  -.7269815
          7 "A I A Engineering Ltd."     592  .4254062  1590.68  -.7309081
          7 "A I A Engineering Ltd."     593 -.0580311  2267.36 -.33348125
          8 "Aban Offshore Ltd."         582 -.1301212 10872.86  .46470425
          8 "Aban Offshore Ltd."         583 -.1686682  9458.07  .18721692
          8 "Aban Offshore Ltd."         584 -.0145899  7862.79  .08449736
          8 "Aban Offshore Ltd."         585  -.611874   7748.9  -.2840065
          8 "Aban Offshore Ltd."         586 -.1288336  3007.55  -.6351557
          8 "Aban Offshore Ltd."         587 -.0308757  2620.08 -1.2225515
          8 "Aban Offshore Ltd."         588 -.2791425  2539.18 -1.4337457
          8 "Aban Offshore Ltd."         589 -.3478934  1830.39   -1.20281
          8 "Aban Offshore Ltd."         590  .2657165  1193.61  -1.608876
          8 "Aban Offshore Ltd."         591  .0192669  1510.77  -1.692288
          8 "Aban Offshore Ltd."         592  1.222536  1539.88 -1.5530345
          8 "Aban Offshore Ltd."         593 -.0119843  3422.43 -1.7080214
          9 "Abbott India Ltd."          582  .0186034   774.07  .03810834
          9 "Abbott India Ltd."          583  .0295522   745.03    .040532
          9 "Abbott India Ltd."          584 -.1340703   767.04 -.03217566
          9 "Abbott India Ltd."          585 -.0898703   664.21 -.00843637
          end
          format %tm mdate
          Last edited by Sartaj Hussain; 10 Aug 2021, 13:46.

          Comment


          • #35
            OK. This is a bit more complicated than before because you want the groupings based on year y to apply to observations in the last half of year y and the first half of year y+1. So I'm using frames to do this--it makes it much easier this way. You'll need version 16 or 17 to run this code. If you are running an earlier version, there are ways to do it without frames, but frankly it's not worth the effort and you should pressure your boss to get you an up-to-date Stata.

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear*
            input float stock_id str51 stock float(mdate rt mcap mom)
            1 "3I Infotech Ltd."           582  .0838774  1300.66 -.12633932
            1 "3I Infotech Ltd."           583  .0435588  1410.03  -.2648224
            1 "3I Infotech Ltd."           584 -.3898757  1471.64 -.17417905
            1 "3I Infotech Ltd."           585  -.389374   898.22  -.1374322
            1 "3I Infotech Ltd."           586 -.2276519    548.5  -.4467936
            1 "3I Infotech Ltd."           587  .1929012   423.63  -.8273377
            1 "3I Infotech Ltd."           588 -.0776197   505.35 -1.1370238
            1 "3I Infotech Ltd."           589 -.1879383   466.13  -.7739682
            1 "3I Infotech Ltd."           590  .1226252   378.52  -.8983014
            1 "3I Infotech Ltd."           591  .4138462   424.94  -.8540121
            1 "3I Infotech Ltd."           592  .5984765    600.8 -1.0945559
            1 "3I Infotech Ltd."           593 -.0251872   960.37 -.58259636
            2 "3M India Ltd."              582 -.0403587  1884.08 -.04965548
            2 "3M India Ltd."              583  .0123053  1808.04 -.00929656
            2 "3M India Ltd."              584 -.1137713  1830.29  -.0061362
            2 "3M India Ltd."              585 -.3538788  1622.06 -.03710564
            2 "3M India Ltd."              586 -.0153705  1048.05  -.3864366
            2 "3M India Ltd."              587  .0108619  1031.94  -.6253247
            2 "3M India Ltd."              588  .0210583  1043.15  -.8057244
            2 "3M India Ltd."              589 -.0546272  1065.11  -.6232147
            2 "3M India Ltd."              590 -.0166135  1006.93  -.6206292
            2 "3M India Ltd."              591  .1656428    990.2  -.6131397
            2 "3M India Ltd."              592  .2114972  1154.22  -.6534325
            2 "3M India Ltd."              593   .035205  1398.33 -.48089325
            3 "63 Moons Technologies Ltd." 582  -.142147  7708.22  -.4747278
            3 "63 Moons Technologies Ltd." 583 -.0119002  6612.52 -.34828845
            3 "63 Moons Technologies Ltd." 584 -.2733497  6533.83  -.4611148
            3 "63 Moons Technologies Ltd." 585 -.5062092  4747.81  -.5940032
            3 "63 Moons Technologies Ltd." 586  .1500146  2344.42  -.8046051
            3 "63 Moons Technologies Ltd." 587 -.1507828  2696.12 -1.2564824
            3 "63 Moons Technologies Ltd." 588 -.0406813  2289.59 -1.1768951
            3 "63 Moons Technologies Ltd." 589 -.1151034  2196.45 -1.1431617
            3 "63 Moons Technologies Ltd." 590  .4634089  1943.63 -1.1361648
            3 "63 Moons Technologies Ltd." 591  .1415551  2844.33 -1.0393578
            3 "63 Moons Technologies Ltd." 592   1.04409  3246.96  -.6623359
            3 "63 Moons Technologies Ltd." 593 -.0962323  6637.07 -.51756567
            4 "A B B India Ltd."           582 -.0510167 17196.36  .01307075
            4 "A B B India Ltd."           583   .140761 16319.06 -.24201564
            4 "A B B India Ltd."           584 -.1065452 18616.15  -.2728126
            4 "A B B India Ltd."           585 -.3175564 16632.69 -.28264767
            4 "A B B India Ltd."           586 -.1824886 11350.87  -.6069741
            4 "A B B India Ltd."           587  .0355104  9279.47  -.9133573
            4 "A B B India Ltd."           588    .05811  9608.99 -1.0667948
            4 "A B B India Ltd."           589 -.2337432 10167.36  -.7721182
            4 "A B B India Ltd."           590  .1625187  7790.81   -.745961
            4 "A B B India Ltd."           591  .1402668  9056.96  -.9950995
            4 "A B B India Ltd."           592  .3331282 10327.35  -.8049828
            4 "A B B India Ltd."           593  .1960904 13767.69  -.5617028
            5 "A B G Shipyard Ltd."        582  .0023809  1710.97   .4182359
            5 "A B G Shipyard Ltd."        583   .076158  1715.05  -.3066918
            5 "A B G Shipyard Ltd."        584 -.1761622  1845.66  -.2124559
            5 "A B G Shipyard Ltd."        585 -.6138647  1520.52  -.3513617
            5 "A B G Shipyard Ltd."        586 -.1548136   587.13  -.7107053
            5 "A B G Shipyard Ltd."        587  .3689071   496.23 -1.6249357
            5 "A B G Shipyard Ltd."        588 -.4745127    679.3 -1.7767352
            5 "A B G Shipyard Ltd."        589  .1875892   356.96 -1.1264615
            5 "A B G Shipyard Ltd."        590 -.0054054   423.92 -1.5662667
            5 "A B G Shipyard Ltd."        591  .3864734   421.63  -1.424076
            5 "A B G Shipyard Ltd."        592  .9551393   584.58 -1.2780082
            5 "A B G Shipyard Ltd."        593  -.113611  1142.94  -.7924519
            6 "A C C Ltd."                 582  .1182775  9804.72 -.27696294
            6 "A C C Ltd."                 583 -.0387643 10964.48  -.6211684
            6 "A C C Ltd."                 584  .0890234 10539.54  -.5085044
            6 "A C C Ltd."                 585 -.1933296  11477.8  -.6682932
            6 "A C C Ltd."                 586 -.1766315  9258.83  -.4810188
            6 "A C C Ltd."                 587  .1763692  7623.52  -.6854388
            6 "A C C Ltd."                 588  .0563926  8969.31  -.8024532
            6 "A C C Ltd."                 589  .0692284  9475.17  -.3900176
            6 "A C C Ltd."                 590  .0682661 10131.12   -.346466
            6 "A C C Ltd."                 591  .1324026 10822.84   -.319372
            6 "A C C Ltd."                 592  .1990812 12255.82  -.1694572
            6 "A C C Ltd."                 593 -.0180076 14696.14  .09212224
            7 "A I A Engineering Ltd."     582  .0162214  2363.88 -.06549466
            7 "A I A Engineering Ltd."     583  .2260563  2402.23 -.13198939
            7 "A I A Engineering Ltd."     584 -.1385219  2945.27 -.04077616
            7 "A I A Engineering Ltd."     585 -.4228988  2537.28  .24033707
            7 "A I A Engineering Ltd."     586 -.1980103  1464.27 -.00271862
            7 "A I A Engineering Ltd."     587  .0892358  1174.33  -.4690253
            7 "A I A Engineering Ltd."     588 -.1620132  1279.12  -.7592906
            7 "A I A Engineering Ltd."     589   .018413  1071.89  -.5515879
            7 "A I A Engineering Ltd."     590  .0714593  1091.62  -.8733688
            7 "A I A Engineering Ltd."     591   .359984  1169.63  -.7269815
            7 "A I A Engineering Ltd."     592  .4254062  1590.68  -.7309081
            7 "A I A Engineering Ltd."     593 -.0580311  2267.36 -.33348125
            8 "Aban Offshore Ltd."         582 -.1301212 10872.86  .46470425
            8 "Aban Offshore Ltd."         583 -.1686682  9458.07  .18721692
            8 "Aban Offshore Ltd."         584 -.0145899  7862.79  .08449736
            8 "Aban Offshore Ltd."         585  -.611874   7748.9  -.2840065
            8 "Aban Offshore Ltd."         586 -.1288336  3007.55  -.6351557
            8 "Aban Offshore Ltd."         587 -.0308757  2620.08 -1.2225515
            8 "Aban Offshore Ltd."         588 -.2791425  2539.18 -1.4337457
            8 "Aban Offshore Ltd."         589 -.3478934  1830.39   -1.20281
            8 "Aban Offshore Ltd."         590  .2657165  1193.61  -1.608876
            8 "Aban Offshore Ltd."         591  .0192669  1510.77  -1.692288
            8 "Aban Offshore Ltd."         592  1.222536  1539.88 -1.5530345
            8 "Aban Offshore Ltd."         593 -.0119843  3422.43 -1.7080214
            9 "Abbott India Ltd."          582  .0186034   774.07  .03810834
            9 "Abbott India Ltd."          583  .0295522   745.03    .040532
            9 "Abbott India Ltd."          584 -.1340703   767.04 -.03217566
            9 "Abbott India Ltd."          585 -.0898703   664.21 -.00843637
            end
            format %tm mdate
            
            gen moy = month(dofm(mdate))
            gen year = year(dofm(mdate))
            //  CREATE A "FISCAL YEAR" RUNNING FROM JULY THROUGH SUBSEQUENT JUNE
            gen fyear = cond(moy > 6, year+1, year)
            
            //  MEDIAN SPLIT BASED ON JUNE VALUE OF mcap
            capture program drop one_year_median_split
            program define one_year_median_split
                xtile june_mcap_group = mcap, nq(2)
                exit
            end
            frame put stock_id year mcap if moy == 6, into(median_split)
            frame change median_split
            runby one_year_median_split, by(year)
            frame change default
            frlink m:1 stock_id fyear, frame(median_split stock_id year)
            frget june_mcap_group, from(median_split)
            frame drop median_split
            drop median_split
            
            //  NOW SPLIT AT 30TH AND 70TH PERCENTILES OF MOM
            capture program drop one_year_three_groups
            program define one_year_three_groups
                _pctile mom, percentiles(30 70)
                gen cut = `r(r1)' in 1
                replace cut = `r(r2)' in 2
                xtile march_mom_group = mom, cutpoints(cut)
                exit
            end
            frame put stock_id year mom if moy == 3, into(three_groups)
            frame change three_groups
            runby one_year_three_groups, by(year) verbose
            frame change default
            frlink m:1 stock_id fyear, frame(three_groups stock_id year)
            frget march_mom_group, from(three_groups)
            frame drop three_groups
            drop three_groups
            
            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(fyear june_mcap_group march_mom_group)
            Added:

            Wait, the above is wrong. The July-June years are drawing on the March and June observations that are within them, rather than preceding them. So it should be:
            Code:
            gen moy = month(dofm(mdate))
            gen year = year(dofm(mdate))
            //  CREATE A "FISCAL YEAR" RUNNING FROM JULY THROUGH SUBSEQUENT JUNE
            gen fyear = cond(moy > 6, year, year-1)
            
            //  MEDIAN SPLIT BASED ON JUNE VALUE OF mcap
            capture program drop one_year_median_split
            program define one_year_median_split
                xtile june_mcap_group = mcap, nq(2)
                exit
            end
            frame put stock_id year mcap if moy == 6, into(median_split)
            frame change median_split
            runby one_year_median_split, by(year)
            frame change default
            frlink m:1 stock_id fyear, frame(median_split stock_id year)
            frget june_mcap_group, from(median_split)
            frame drop median_split
            drop median_split
            
            //  NOW SPLIT AT 30TH AND 70TH PERCENTILES OF MOM
            capture program drop one_year_three_groups
            program define one_year_three_groups
                _pctile mom, percentiles(30 70)
                gen cut = `r(r1)' in 1
                replace cut = `r(r2)' in 2
                xtile march_mom_group = mom, cutpoints(cut)
                exit
            end
            frame put stock_id year mom if moy == 3, into(three_groups)
            frame change three_groups
            runby one_year_three_groups, by(year) verbose
            frame change default
            frlink m:1 stock_id fyear, frame(three_groups stock_id year)
            frget march_mom_group, from(three_groups)
            frame drop three_groups
            drop three_groups
            
            capture program drop one_weighted_return
            program define one_weighted_return
                if !missing(june_mcap_group, march_mom_group) {
                    egen numerator = total(mcap*rt)
                    egen denominator = total(mcap)
                    gen vw_mean_rt = numerator/denominator
                }
                exit
            end
            
            runby one_weighted_return, by(fyear june_mcap_group march_mom_group)
            The results with the empty data are all missing values because all of the example data start in July 2008, and no data from March or June 2008 are available to do the partitioning into groups. In the real data, presumably the time extension of the data is longer and you will only get missing values for the first of the years.
            Last edited by Clyde Schechter; 10 Aug 2021, 14:54.

            Comment


            • #36
              I try to run the code but this line returns the following error:

              Code:
              frame put stock_id year mcap if moy == 6, into(median_split)
              Error: if not allowed
              r(101);

              Comment


              • #37
                I don't know what to tell you. The code ran without error messages on my computer. And the syntax of the -frame put- command does allow -if- conditions. I'm wondering if the error message is a misleading one that is triggered by something else. For example, the frame specified in the -into()- option of -frame put- must not already exist. If you ran the code before, and then tried to re-run it without clearing frames, you would violate that condition, and perhaps the error message is just misleading. (So, make sure you -clear*- before running the code.)

                Also, is the code you are showing in #36 the exact code you ran, with no edits or modifications whatsoever? If the comma were misplaced before the -if- that might cause the error you show. If I recall correctly, we have had situations before where you modified my code and the modifications did not work correctly. So is this exactly what you ran?

                Comment


                • #38
                  Yes absolutely, I use STATA16 ver. I copied the same code in #35 without any changes and use the same data set as in example. The line in code is correct but i do not get why it is showing error and hence running of code doesn't proceed further. Could you please re-run it on your side.

                  Comment


                  • #39
                    Not able to resolve the issue still.

                    Comment


                    • #40
                      As you can see from the Results below, it runs without error messages on my setup:
                      Code:
                      . * Example generated by -dataex-. To install: ssc install dataex
                      . clear*
                      
                      . input float stock_id str51 stock float(mdate rt mcap mom)
                      
                            stock_id                                                stock      mdate         rt       mcap        mom
                        1. 1 "3I Infotech Ltd."           582  .0838774  1300.66 -.12633932
                        2. 1 "3I Infotech Ltd."           583  .0435588  1410.03  -.2648224
                        3. 1 "3I Infotech Ltd."           584 -.3898757  1471.64 -.17417905
                        4. 1 "3I Infotech Ltd."           585  -.389374   898.22  -.1374322
                        5. 1 "3I Infotech Ltd."           586 -.2276519    548.5  -.4467936
                        6. 1 "3I Infotech Ltd."           587  .1929012   423.63  -.8273377
                        7. 1 "3I Infotech Ltd."           588 -.0776197   505.35 -1.1370238
                        8. 1 "3I Infotech Ltd."           589 -.1879383   466.13  -.7739682
                        9. 1 "3I Infotech Ltd."           590  .1226252   378.52  -.8983014
                       10. 1 "3I Infotech Ltd."           591  .4138462   424.94  -.8540121
                       11. 1 "3I Infotech Ltd."           592  .5984765    600.8 -1.0945559
                       12. 1 "3I Infotech Ltd."           593 -.0251872   960.37 -.58259636
                       13. 2 "3M India Ltd."              582 -.0403587  1884.08 -.04965548
                       14. 2 "3M India Ltd."              583  .0123053  1808.04 -.00929656
                       15. 2 "3M India Ltd."              584 -.1137713  1830.29  -.0061362
                       16. 2 "3M India Ltd."              585 -.3538788  1622.06 -.03710564
                       17. 2 "3M India Ltd."              586 -.0153705  1048.05  -.3864366
                       18. 2 "3M India Ltd."              587  .0108619  1031.94  -.6253247
                       19. 2 "3M India Ltd."              588  .0210583  1043.15  -.8057244
                       20. 2 "3M India Ltd."              589 -.0546272  1065.11  -.6232147
                       21. 2 "3M India Ltd."              590 -.0166135  1006.93  -.6206292
                       22. 2 "3M India Ltd."              591  .1656428    990.2  -.6131397
                       23. 2 "3M India Ltd."              592  .2114972  1154.22  -.6534325
                       24. 2 "3M India Ltd."              593   .035205  1398.33 -.48089325
                       25. 3 "63 Moons Technologies Ltd." 582  -.142147  7708.22  -.4747278
                       26. 3 "63 Moons Technologies Ltd." 583 -.0119002  6612.52 -.34828845
                       27. 3 "63 Moons Technologies Ltd." 584 -.2733497  6533.83  -.4611148
                       28. 3 "63 Moons Technologies Ltd." 585 -.5062092  4747.81  -.5940032
                       29. 3 "63 Moons Technologies Ltd." 586  .1500146  2344.42  -.8046051
                       30. 3 "63 Moons Technologies Ltd." 587 -.1507828  2696.12 -1.2564824
                       31. 3 "63 Moons Technologies Ltd." 588 -.0406813  2289.59 -1.1768951
                       32. 3 "63 Moons Technologies Ltd." 589 -.1151034  2196.45 -1.1431617
                       33. 3 "63 Moons Technologies Ltd." 590  .4634089  1943.63 -1.1361648
                       34. 3 "63 Moons Technologies Ltd." 591  .1415551  2844.33 -1.0393578
                       35. 3 "63 Moons Technologies Ltd." 592   1.04409  3246.96  -.6623359
                       36. 3 "63 Moons Technologies Ltd." 593 -.0962323  6637.07 -.51756567
                       37. 4 "A B B India Ltd."           582 -.0510167 17196.36  .01307075
                       38. 4 "A B B India Ltd."           583   .140761 16319.06 -.24201564
                       39. 4 "A B B India Ltd."           584 -.1065452 18616.15  -.2728126
                       40. 4 "A B B India Ltd."           585 -.3175564 16632.69 -.28264767
                       41. 4 "A B B India Ltd."           586 -.1824886 11350.87  -.6069741
                       42. 4 "A B B India Ltd."           587  .0355104  9279.47  -.9133573
                       43. 4 "A B B India Ltd."           588    .05811  9608.99 -1.0667948
                       44. 4 "A B B India Ltd."           589 -.2337432 10167.36  -.7721182
                       45. 4 "A B B India Ltd."           590  .1625187  7790.81   -.745961
                       46. 4 "A B B India Ltd."           591  .1402668  9056.96  -.9950995
                       47. 4 "A B B India Ltd."           592  .3331282 10327.35  -.8049828
                       48. 4 "A B B India Ltd."           593  .1960904 13767.69  -.5617028
                       49. 5 "A B G Shipyard Ltd."        582  .0023809  1710.97   .4182359
                       50. 5 "A B G Shipyard Ltd."        583   .076158  1715.05  -.3066918
                       51. 5 "A B G Shipyard Ltd."        584 -.1761622  1845.66  -.2124559
                       52. 5 "A B G Shipyard Ltd."        585 -.6138647  1520.52  -.3513617
                       53. 5 "A B G Shipyard Ltd."        586 -.1548136   587.13  -.7107053
                       54. 5 "A B G Shipyard Ltd."        587  .3689071   496.23 -1.6249357
                       55. 5 "A B G Shipyard Ltd."        588 -.4745127    679.3 -1.7767352
                       56. 5 "A B G Shipyard Ltd."        589  .1875892   356.96 -1.1264615
                       57. 5 "A B G Shipyard Ltd."        590 -.0054054   423.92 -1.5662667
                       58. 5 "A B G Shipyard Ltd."        591  .3864734   421.63  -1.424076
                       59. 5 "A B G Shipyard Ltd."        592  .9551393   584.58 -1.2780082
                       60. 5 "A B G Shipyard Ltd."        593  -.113611  1142.94  -.7924519
                       61. 6 "A C C Ltd."                 582  .1182775  9804.72 -.27696294
                       62. 6 "A C C Ltd."                 583 -.0387643 10964.48  -.6211684
                       63. 6 "A C C Ltd."                 584  .0890234 10539.54  -.5085044
                       64. 6 "A C C Ltd."                 585 -.1933296  11477.8  -.6682932
                       65. 6 "A C C Ltd."                 586 -.1766315  9258.83  -.4810188
                       66. 6 "A C C Ltd."                 587  .1763692  7623.52  -.6854388
                       67. 6 "A C C Ltd."                 588  .0563926  8969.31  -.8024532
                       68. 6 "A C C Ltd."                 589  .0692284  9475.17  -.3900176
                       69. 6 "A C C Ltd."                 590  .0682661 10131.12   -.346466
                       70. 6 "A C C Ltd."                 591  .1324026 10822.84   -.319372
                       71. 6 "A C C Ltd."                 592  .1990812 12255.82  -.1694572
                       72. 6 "A C C Ltd."                 593 -.0180076 14696.14  .09212224
                       73. 7 "A I A Engineering Ltd."     582  .0162214  2363.88 -.06549466
                       74. 7 "A I A Engineering Ltd."     583  .2260563  2402.23 -.13198939
                       75. 7 "A I A Engineering Ltd."     584 -.1385219  2945.27 -.04077616
                       76. 7 "A I A Engineering Ltd."     585 -.4228988  2537.28  .24033707
                       77. 7 "A I A Engineering Ltd."     586 -.1980103  1464.27 -.00271862
                       78. 7 "A I A Engineering Ltd."     587  .0892358  1174.33  -.4690253
                       79. 7 "A I A Engineering Ltd."     588 -.1620132  1279.12  -.7592906
                       80. 7 "A I A Engineering Ltd."     589   .018413  1071.89  -.5515879
                       81. 7 "A I A Engineering Ltd."     590  .0714593  1091.62  -.8733688
                       82. 7 "A I A Engineering Ltd."     591   .359984  1169.63  -.7269815
                       83. 7 "A I A Engineering Ltd."     592  .4254062  1590.68  -.7309081
                       84. 7 "A I A Engineering Ltd."     593 -.0580311  2267.36 -.33348125
                       85. 8 "Aban Offshore Ltd."         582 -.1301212 10872.86  .46470425
                       86. 8 "Aban Offshore Ltd."         583 -.1686682  9458.07  .18721692
                       87. 8 "Aban Offshore Ltd."         584 -.0145899  7862.79  .08449736
                       88. 8 "Aban Offshore Ltd."         585  -.611874   7748.9  -.2840065
                       89. 8 "Aban Offshore Ltd."         586 -.1288336  3007.55  -.6351557
                       90. 8 "Aban Offshore Ltd."         587 -.0308757  2620.08 -1.2225515
                       91. 8 "Aban Offshore Ltd."         588 -.2791425  2539.18 -1.4337457
                       92. 8 "Aban Offshore Ltd."         589 -.3478934  1830.39   -1.20281
                       93. 8 "Aban Offshore Ltd."         590  .2657165  1193.61  -1.608876
                       94. 8 "Aban Offshore Ltd."         591  .0192669  1510.77  -1.692288
                       95. 8 "Aban Offshore Ltd."         592  1.222536  1539.88 -1.5530345
                       96. 8 "Aban Offshore Ltd."         593 -.0119843  3422.43 -1.7080214
                       97. 9 "Abbott India Ltd."          582  .0186034   774.07  .03810834
                       98. 9 "Abbott India Ltd."          583  .0295522   745.03    .040532
                       99. 9 "Abbott India Ltd."          584 -.1340703   767.04 -.03217566
                      100. 9 "Abbott India Ltd."          585 -.0898703   664.21 -.00843637
                      101. end
                      
                      . format %tm mdate
                      
                      .
                      . gen moy = month(dofm(mdate))
                      
                      . gen year = year(dofm(mdate))
                      
                      . //  CREATE A "FISCAL YEAR" RUNNING FROM JULY THROUGH SUBSEQUENT JUNE
                      . gen fyear = cond(moy > 6, year, year-1)
                      
                      .
                      . //  MEDIAN SPLIT BASED ON JUNE VALUE OF mcap
                      . capture program drop one_year_median_split
                      
                      . program define one_year_median_split
                        1.     xtile june_mcap_group = mcap, nq(2)
                        2.     exit
                        3. end
                      
                      . frame put stock_id year mcap if moy == 6, into(median_split)
                      
                      . frame change median_split
                      
                      . runby one_year_median_split, by(year)
                      
                      --------------------------------------
                      Number of by-groups    =             1
                      by-groups with errors  =             0
                      by-groups with no data =             0
                      Observations processed =             8
                      Observations saved     =             8
                      --------------------------------------
                      
                      . frame change default
                      
                      . frlink m:1 stock_id fyear, frame(median_split stock_id year)
                        (no observations in default matched!)
                      
                      . frget june_mcap_group, from(median_split)
                      (100 missing values generated)
                        (1 variable copied from linked frame)
                      
                      . frame drop median_split
                      
                      . drop median_split
                      
                      .
                      . //  NOW SPLIT AT 30TH AND 70TH PERCENTILES OF MOM
                      . capture program drop one_year_three_groups
                      
                      . program define one_year_three_groups
                        1.     _pctile mom, percentiles(30 70)
                        2.     gen cut = `r(r1)' in 1
                        3.     replace cut = `r(r2)' in 2
                        4.     xtile march_mom_group = mom, cutpoints(cut)
                        5.     exit
                        6. end
                      
                      . frame put stock_id year mom if moy == 3, into(three_groups)
                      
                      . frame change three_groups
                      
                      . runby one_year_three_groups, by(year) verbose
                      (7 missing values generated)
                      (1 real change made)
                      
                      --------------------------------------
                      Number of by-groups    =             1
                      by-groups with errors  =             0
                      by-groups with no data =             0
                      Observations processed =             8
                      Observations saved     =             8
                      --------------------------------------
                      
                      . frame change default
                      
                      . frlink m:1 stock_id fyear, frame(three_groups stock_id year)
                        (no observations in default matched!)
                      
                      . frget march_mom_group, from(three_groups)
                      (100 missing values generated)
                        (1 variable copied from linked frame)
                      
                      . frame drop three_groups
                      
                      . drop three_groups
                      
                      .
                      . capture program drop one_weighted_return
                      
                      . program define one_weighted_return
                        1.     if !missing(june_mcap_group, march_mom_group) {
                        2.         egen numerator = total(mcap*rt)
                        3.         egen denominator = total(mcap)
                        4.         gen vw_mean_rt = numerator/denominator
                        5.     }
                        6.     exit
                        7. end
                      
                      .
                      . runby one_weighted_return, by(fyear june_mcap_group march_mom_group)
                      
                      --------------------------------------
                      Number of by-groups    =             1
                      by-groups with errors  =             0
                      by-groups with no data =             0
                      Observations processed =           100
                      Observations saved     =           100
                      --------------------------------------
                      
                      .
                      end of do-file
                      I have one more thought. Sometimes the Forum editor or other text processing programs (Word is a notorious offender in this regard) insert non-printing characters into text. Perhaps there is such a character before, within, or after the "if" that Stata is objecting to. You won't see it with your eye, but to Stata it will be unavoidably there. Try deleting that one line of code and retyping it in by hand, being careful not to introduce any typos as you do so. If the line runs now, that would confirm this explanation. If this is what is happening, you may find other such non-printing characters scattered through the code, causing Stata to throw syntax error messages at all sorts of things that look, to human eyes, perfectly OK. It's a pain when that happens. I have sometimes found that if I copy the code from the Forum editor into notepad (or some other barebones text editor), save it as a .txt file, close and re-open it, and then copy from there to the do-file editor, that the offending characters get laundered out. So that might be worth a try.

                      If that isn't the problem, I really have no other thoughts on how to help you with this.

                      Comment


                      • #41
                        Isn't it that if operator doesn't work because i try it in STATA 16 and you use STATA 17.

                        Comment


                        • #42
                          No, that's not the issue. While I am running Stata 17 for day to day work, I still have 16 installed on my computer and when I run this code on 16 it runs without error messages--it works exactly the way it does on 17. And the online PDF manuals for version 16 do show that -frame put- supports -if- conditions. So that's not the issue.

                          Have you tried the "standard" solutions to odd program behavior: run -update query- to see if you have the latest update to version 16, and update it if you don't. Shut down Stata, re-launch it and try again. If that doesn't work, do a cold reboot of your computer and try again.

                          Comment


                          • #43
                            Thank you for your efforts. I appreciate and put them on record. However, it did not work in ver.16. May be my ver. 16 is not updated. It might work with ver.16.1 perhaps. But i got now ver.17 with which it worked nicely.

                            Now, the code needs to be updated further. The following changes would make the code # 35 (correct one) more useful:

                            1. keep one observation each month for each of the 2*3 or 6 groups of vw_mean_rt.
                            2. drop if vw_mean_rt is missing.
                            3. For each month, create two more variables SMB and HML. The definition of SMB and HML is given below:

                            SMB = (SL+SM+SH)/3 - (BL+BM+BH)/3 and

                            HML = (SH+BH)/2 - (SL+BL)/2

                            SMB is the simple average of value weighted (rt) of three small mcap groups minus the simple average of value weighted (rt) of three big mcap groups. HML is the simple average of value weighted (rt) two high mom groups minus the simple average of value weighted (rt) of two low mom groups. S implies small and B implies big. L, M and H respectively mean low, medium and high. Small and Big is associated with two groups of value weighted (rt) sliced on the basis of mcap. Low, Medium and High is associated with three groups of value weighted (rt) partitioned on the basis mom. I would try to define these groups as per the information from code in # 35 (corrected version)

                            SL = if june_mcap_group = 1 & march_mom_group =1
                            SM = if june_mcap_group = 1 & march_mom_group = 2
                            SH = if june_mcap_group = 1 & march_mom_group = 3
                            BL = if june_mcap_group = 2 & march_mom_group = 1
                            BM = if june_mcap_group = 2 & march_mom_group = 2
                            BH = if june_mcap_group = 2 & march_mom_group = 3

                            Please confirm if this is correctly related. The code may please be update for these additions.
                            Last edited by Sartaj Hussain; 13 Aug 2021, 08:37.

                            Comment


                            • #44
                              I think I understand this, but I have a couple of questions.

                              I don't understand why you are going to one observation per month. The way that vw_mean_rt was created, it is constant (within groups) over the course of a year. So if you do this by month, you will have 12 copies of the same values. This repetition doesn't make sense to me. Did you mean to reduce to one observation for each of the 6 groups per fyear? (Or was the calculation of vw_mean_rt supposed to have been done by month and not by fyear?) In the code below, I assume you meant to do this for one observation per group per year, not per month.

                              Next, the calculation of SMB draws on all of the data. But the calculation of HML ignores the data in the middle march_mom_group. Is that intentional?

                              Code:
                              by june_mcap_group march_mom_group fyear (mdate), sort: assert vw_mean_rt[1] == vw_mean_rt[_N]
                              by june_mcap_group march_mom_group fyear: keep if _n == 1
                              drop if missing(vw_mean_rt)
                              keep fyear june_mcap_group march_mom_group vw_mean_rt
                              
                              isid june_mcap_group march_mom_group fyear, sort
                              by fyear june_mcap_group, sort: egen temp = mean(vw_mean_rt)
                              by fyear (june_mcap_group), sort: gen SMB = temp[1] - temp[_N]
                              drop temp
                              
                              by fyear march_mom_group, sort: egen temp = mean(vw_mean_rt)
                              by fyear (march_mom_group): gen HML = temp[1] - temp[_N]
                              drop temp

                              Comment


                              • #45
                                I don't understand why you are going to one observation per month. The way that vw_mean_rt was created, it is constant (within groups) over the course of a year. So if you do this by month, you will have 12 copies of the same values. This repetition doesn't make sense to me. Did you mean to reduce to one observation for each of the 6 groups per fyear? (Or was the calculation of vw_mean_rt supposed to have been done by month and not by fyear?) In the code below, I assume you meant to do this for one observation per group per year, not per month.
                                Let me clarify. The grouping only is to be done once in a year. While as the calculation of vw_mean_rt is to be done by month and not by fyear. So, there won't be any repeated values in that case.

                                Next, the calculation of SMB draws on all of the data. But the calculation of HML ignores the data in the middle march_mom_group. Is that intentional?
                                Well, this is how the method of calculation of HML is in the literature.

                                Keeping in view this, the overall code need to be updated once again to get changes into effect.
                                Last edited by Sartaj Hussain; 13 Aug 2021, 11:01.

                                Comment

                                Working...
                                X