Announcement

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

  • Problem with bysort command as it is always showing not sorted!

    Dear Stata Members
    I would like to calculate the industry sales growth (isg) and following is my sample data


    Code:
    clear
    input str9 firm int year byte ind_dum float netsales
    "4DS.AX"   2015 31 7.70e-06
    "PRS.AX"   2015 21        .
    "HRN.AX"   2015 21        .
    "VIC.AX"   2015 21  .000139
    "PVW.AX"   2015 21        0
    "ALX.AX"   2015 23  1.88932
    "FBR.AX"   2015 31  .174572
    "IPT.AX"   2015 21  .927405
    "ODM.AX"   2015 21  .018043
    "ABV.AX"   2015 31  5.31421
    "SRK.AX"   2015 21  .193432
    "KLO.AX"   2015 23  13.2431
    "GLA.AX"   2015 21  .009091
    "EOS.AX"   2015 31  22.1893
    "BLY.AX"   2015 21  735.158
    "AGR.AX"   2015 21  .030962
    "DRE.AX"   2015 21  .015323
    "LLC.AX"   2015 23  10231.6
    "NZS.AX"   2015 31  .477879
    "TPD.AX"   2015 21  .298108
    "HGO.AX"   2015 21  101.487
    "AWV.AX"   2015 21  .016317
    "ALT.AX"   2015 31  .862368
    "IMC.AX"   2015 31  .865704
    "SYA.AX"   2015 21  .016271
    "TNR.AX"   2015 21        0
    "FLC.AX"   2015 31   .00118
    "NWC.AX"   2015 21  .034922
    "ADR.AX"   2015 31  2.21078
    "AAP.AX"   2015 11   2.6729
    "HPP.AX"   2015 31  54.7183
    "CIO.AX"   2015 31        0
    "AGD.AX"   2015 21  62.4951
    "BMN.AX"   2015 21   .05778
    "TGN.AX"   2015 21  .120552
    "PXX.AX"   2015 21  .002889
    "RIO.AX"   2015 21    34829
    "AJQ.AX"   2015 21  .075345
    "PDI.AX"   2015 21  .007142
    "PSA.AX"   2015 21    1.613
    "GGG.AX"   2015 21  .141135
    "CCE.AX"   2015 22  1.25232
    "PDZ.AX"   2015 21  .026895
    "CRR.AX"   2015 21   .14729
    "PPG.AX"   2015 31  187.885
    "AQX.AX"   2015 21  .000917
    "EUR.AX"   2015 21  .003143
    "SVL.AX"   2015 21  .002712
    "CCZ.AX"   2015 21  .004715
    "FNT.AX"   2015 21  .015054
    "LRL.AX"   2015 21  .001579
    "TMS.AX"   2015 21        .
    "SIH.AX"   2015 21  .001418
    "APG.AX"   2015 21        .
    "CNJ.AX"   2015 21  .000855
    "EMU.AX"   2015 21  .166837
    "XAM.AX"   2015 21  .026918
    "FIN.AX"   2015 21        .
    "CLA.AX"   2015 21  .002042
    "GUD.AX"   2015 31  382.961
    "DYL.AX"   2015 21  .097517
    "DEV.AX"   2015 21        .
    "AAU.AX"   2015 21  46.6495
    "JAL.AX"   2015 21  .012966
    "AAR.AX"   2015 21        0
    "GLV.AX"   2015 21  .164472
    "AZS.AX"   2015 21  .275887
    "LCL.AX"   2015 21  .003237
    "SFX.AX"   2015 21  .164811
    "LEG.AX"   2015 21  .418429
    "LCD.AX"   2015 21  .008343
    "ADD.AX"   2015 21        0
    "BSR.AX"   2015 21  .015278
    "BKW.AX"   2015 31   528.38
    "NVA.AX"   2015 21        0
    "IDA.AX"   2015 21  2.86434
    "PPY.AX"   2015 31  .068049
    "EQR.AX"   2015 21  .040662
    "HE8.AX"   2015 21        .
    "KRR.AX"   2015 21  .003197
    "ARD.AX"   2015 21        .
    "SES.AX"   2015 31  13.8294
    "SAN.AX"   2015 21     .047
    "BNR.AX"   2015 21  .021363
    "MSV.AX"   2015 21  19.3945
    "CEL.AX"   2015 21  .044144
    "MPR.AX"   2015 22  41.8527
    "DEM.AX"   2015 22  .528381
    "AS1.AX"   2015 11  1.25869
    "LTR.AX"   2015 21  .383828
    "VML.AX"   2015 21  .035893
    "WNR.AX"   2015 31  2.20463
    "MLM.AX"   2015 21  .552067
    "MGT.AX"   2015 21  .056362
    "DDD.AX"   2015 21   .00785
    "XRF.AX"   2015 31  15.9248
    "A3D.AX"   2015 31        .
    "GLN.AX"   2015 21  .021841
    "OKJ.AX"   2015 31  .011024
    "FWD.AX"   2015 31  210.239
    "RIC.AX"   2015 31  699.422
    "ZEU.AX"   2015 21  .137385
    "RFG.AX"   2015 31  190.477
    "MNB.AX"   2015 21   .00235
    "MXC.AX"   2015 31  .004538
    "GMRDD.AX" 2015 21  .019976
    "ERA.AX"   2015 21  253.359
    "NXM.AX"   2015 21        0
    "WC8.AX"   2015 21  .001402
    "CZR.AX"   2015 21  .007365
    "ESS.AX"   2015 21  .201089
    "AYM.AX"   2015 21  .037542
    "MYL.AX"   2015 21  .166144
    "KOV.AX"   2015 31  48.5543
    "ZGL.AX"   2015 31  92.6665
    "AUH.AX"   2015 21  .013012
    "VMS.AX"   2015 21  .134612
    "BUY.AX"   2015 21  1.49285
    "GAP.AX"   2015 31  114.014
    "CRB.AX"   2015 21  .065337
    "BRI.AX"   2015 31  123.384
    "GW1.AX"   2015 21  .098519
    "BUX.AX"   2015 21  .011232
    "ECS.AX"   2015 31  4.54599
    "WSI.AX"   2015 23  .000116
    "88E.AX"   2015 21  .010549
    "AJY.AX"   2015 21  1.93232
    "E25.AX"   2015 21  .334176
    "ERL.AX"   2015 21  .003112
    "SI6.AX"   2015 21  .055345
    "CSL.AX"   2015 31     5628
    "ATL.AX"   2015 31  72.7194
    "BKL.AX"   2015 31  363.331
    "MBK.AX"   2015 21  .004946
    "PBX.AX"   2015 21  .038497
    "MVL.AX"   2015 21        0
    "ANW.AX"   2015 21  .001009
    "EGR.AX"   2015 31  .038173
    "NWF.AX"   2015 21   .05033
    "PEN.AX"   2015 21     .153
    "WPL.AX"   2015 21     5030
    "DRX.AX"   2015 21  .111577
    "SGC.AX"   2015 21  .270732
    "GLL.AX"   2015 21  .615548
    "IBG.AX"   2015 21  .196452
    "EMP.AX"   2015 21  .287859
    "SKS.AX"   2015 23  2.71821
    "PEK.AX"   2015 21  .029606
    "AWN.AX"   2015 22   3.6738
    "M7T.AX"   2015 31  .128718
    "AUZ.AX"   2015 21        .
    "TIE.AX"   2015 21        .
    "EVN.AX"   2015 21  513.053
    "COI.AX"   2015 21  .593977
    "TON.AX"   2015 21  .000546
    "AOP.AX"   2015 21  .016479
    "FHS.AX"   2015 21  .000755
    "PNV.AX"   2015 31   .10841
    "AEV.AX"   2015 21  .550018
    "BXB.AX"   2015 31   5440.5
    "CGB.AX"   2015 31  .131854
    "RSG.AX"   2015 21   381.81
    "ASQ.AX"   2015 21  1.22065
    "FFI.AX"   2015 31   23.669
    "EL8.AX"   2015 21  .289801
    "MWY.AX"   2015 31  109.614
    "AQI.AX"   2015 21   .01681
    "PRX.AX"   2015 21  .302281
    "RR1.AX"   2015 21  .005855
    "EGY.AX"   2015 31  8.32588
    "RNX.AX"   2015 21  .012149
    "FML.AX"   2015 21  1.82021
    "RPG.AX"   2015 23        0
    "CHK.AX"   2015 31  .010146
    "MCE.AX"   2015 31  110.995
    "AFR.AX"   2015 21    .0705
    "HLX.AX"   2015 21  .055592
    "GSN.AX"   2015 21  .030639
    "VRX.AX"   2015 21  .047102
    "GED.AX"   2015 21  .131484
    "GOR.AX"   2015 21  .770753
    "BMG.AX"   2015 21  .003821
    "BLG.AX"   2015 31  2.72176
    "GIB.AX"   2015 21   .07533
    "ADN.AX"   2015 21  .036463
    "ECT.AX"   2015 21  .009676
    "ZIM.AX"   2015 21  408.391
    "CCJ.AX"   2015 21  .011348
    "MAT.AX"   2015 21  1.51972
    "WMC.AX"   2015 21  .009707
    "BAS.AX"   2015 21  .057037
    "TNP.AX"   2015 21  .004053
    "TZN.AX"   2015 21        0
    "KAR.AX"   2015 21   1.6671
    "TTT.AX"   2015 31        0
    "CAE.AX"   2015 21  .005971
    "AIS.AX"   2015 21  167.395
    "STO.AX"   2015 21     2442
    "CZN.AX"   2015 21  1.08749
    "E79.AX"   2015 21  .074582
    "CGN.AX"   2015 21   .04392
    "MGX.AX"   2015 21  252.577
    "FMG.AX"   2015 21     8574
    "AVA.AX"   2015 31   14.371
    "RAN.AX"   2015 31   .34746
    "CYC.AX"   2015 31   9.1874
    "ENR.AX"   2015 21  1.23003
    "TOE.AX"   2015 21  .382333
    "KPO.AX"   2015 22  .775005
    "SYR.AX"   2015 21     .291
    "GCR.AX"   2015 21  .030816
    "AKN.AX"   2015 21  .031552
    "MBH.AX"   2015 31  .007951
    "RDN.AX"   2015 21  43.7964
    "FAU.AX"   2015 21  .004569
    "CHN.AX"   2015 21  .468602
    "MML.AX"   2015 21  123.172
    "NCR.AX"   2015 21  .396755
    "BLU.AX"   2015 21  .161784
    "EYE.AX"   2015 31  48.2909
    "GLB.AX"   2015 31  106.131
    "OBM.AX"   2015 21  .001872
    "ARR.AX"   2015 21  .003944
    "SKY.AX"   2015 21  .244236
    "ICG.AX"   2015 21  .026116
    "SGQ.AX"   2015 21  .016047
    "INA.AX"   2015 23  58.5664
    "STX.AX"   2015 21  2.07699
    "AGS.AX"   2015 21  .170589
    "TNG.AX"   2015 21        .
    "OKU.AX"   2015 21  .505905
    "CMM.AX"   2015 21  .214625
    "PO3.AX"   2015 22        0
    "AME.AX"   2015 21  .026317
    "NMT.AX"   2015 21  .323205
    "BDC.AX"   2015 21  .086285
    "MAN.AX"   2015 21  2.41912
    "PGM.AX"   2015 21  .013397
    "KGM.AX"   2015 21  .007165
    "DAF.AX"   2015 21  .001071
    "ING.AX"   2015 11   1740.1
    "RXM.AX"   2015 21        .
    "KFE.AX"   2015 21   .01047
    "PTX.AX"   2015 31  .030793
    "ENT.AX"   2015 21  .012742
    "GES.AX"   2015 21   .00077
    "VAR.AX"   2015 21  .206112
    "CYL.AX"   2015 21  .015516
    "CMP.AX"   2015 31  25.8045
    "SIX.AX"   2015 31  .973876
    end

    Code:
    encode firm, gen (id)
    
    xtset id year
    
    Panel variable: id (strongly balanced)
     Time variable: year, 2015 to 2015
             Delta: 1 unit
    
    
     by ind_dum year, sort:  gen isg = d.netsales /l.netsales                                   
    not sorted
    r(5);
    
    *I also tried 
    bysort ind_dum (year): gen isg = d.netsales /l.netsales
    not sorted
    r(5);
    Why is Stata repeatedly saying not sorted?

  • #2
    Your problem is that you are giving Stata contradictory instructions. That becomes Stata's problem.

    xtset implies that there is one and only one way to sort the dataset properly, on panel identifier and then time variable.

    SImilarly your use of time series operators implies the same thing.

    But once you sort on ind_dum and then year you have insisted on a different sort order.. The effect is then that time series operators can not be applied because the dataset is in the wrong sort order for what you want to do.

    The simple solution is that your variable ind_dum has no bearing on the new variable you want to calculate. So the syntax is irrelevant as well as illegal.

    You should ensure that the data are sorted properly with

    Code:
    xtset 
    and then calculate the new variable

    Code:
     
     gen isg = d.netsales /l.netsales
    Other way round, if you think that ind_dum has a bearing on what you want to calculate, you need different syntax.

    Comment


    • #3
      Thanks Nick Cox for the reply and opening the black box. My main analysis is with the unique firm-year combination. But now I need to calculate industry sales growth. So basically for each industry-year combination, I need to calculate sales growth. How to do that with my existing data?

      I tried
      Code:
      xtset ind_dum
      
      Panel variable: ind_dum (unbalanced)
      
      . by ind_dum  year, sort: gen isg = d.netsales /l.netsales
      time variable not set
      r(111);
      
      . xtset ind_dum year
      repeated time values within panel
      r(451);
      
      . by ind_dum  year, sort: gen isg = d.netsales /l.netsales
      time variable not set
      r(111);
      
      . xtset ind_dum
      
      Panel variable: ind_dum (unbalanced)
      
      . gen isg=d.netsales/l.nsales
      time variable not set
      r(111);
      
      .

      Comment


      • #4
        Industry sales growth I can only understand as being based on total sales for each industry.

        Your data example is not suitable here as only one year is represented.

        But

        Code:
        egen double ind_sales = total(netsales), by(ind_dum year)
        
        gen isg = D.ind_sales / L.ind_sales
        appears to be what you want The result is necessarily identical for each company in the same industry in each year.
        Last edited by Nick Cox; 30 Dec 2021, 06:37.

        Comment


        • #5
          Thanks Nick Cox. It suits my requirement and you are right, I forgot the aspect of total sales. Thanks a lot for the timely help

          Comment

          Working...
          X