Announcement

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

  • Matrix and Vector Manipulation

    The data I have is on the country, sector and time level. Specifically, I am finding the proportion of a sector's manufacturing output to the country's total manufacturing output for each country and year, i.e. group (CTY Time). The format of this data I am constructing is to be a vector. The problem is that mkmat may not be combined with the by command. Would anyone know a way around this? Thank you very much in advance!

  • #2
    You're much more likely to get a quick and useful answer to a question if you post example data using the -dataex- command, as described in the StataList FAQ for new users, which you might well want to read again. Showing some code also is helpful, and in your case, showing the code you have used with -mkmat- would be helpful, too.

    Comment


    • #3
      Thank you very much for the kind reminder Mike!

      The following is a snapshot of the data I am using:
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input long Country int Time long Industry float(id4 ManShare)
      4 1996 13 1   .15671143
      4 1996 24 1           1
      4 1996 10 1  .005093734
      4 1996 12 1   .06771848
      4 1996 22 1  .003159164
      4 1996  2 1   .10667754
      4 1996  8 1 .0020469923
      4 1996  1 1   .42088395
      4 1996  9 1    .1863612
      4 1996  3 1 .0004224119
      4 1996  6 1  .005146236
      4 1997 10 2   .00915482
      4 1997 13 2   .11123684
      4 1997  1 2    .5626194
      4 1997  8 2  .002857935
      4 1997 22 2 .0004250387
      4 1997 12 2  .071257584
      4 1997  2 2    .0498813
      4 1997  9 2    .1573661
      4 1997  3 2  .007754172
      4 1997 24 2           1
      4 1998 24 3           1
      4 1998  9 3   .15857527
      4 1998 12 3   .08016227
      4 1998 10 3  .028434867
      4 1998 22 3 .0035012204
      4 1998  2 3  .030519135
      4 1998  7 3   .05883621
      4 1998 16 3    .0227555
      4 1998  6 3   .07812565
      4 1998  1 3   .21774356
      4 1998  5 3   .11324187
      4 1998  3 3   .09556854
      4 1998 13 3    .1125359
      4 1999  7 4    .0550415
      4 1999  2 4   .02449911
      4 1999  6 4  .036008928
      4 1999 22 4    .0526593
      4 1999 13 4   .11118634
      4 1999 24 4           1
      4 1999 12 4    .0996013
      4 1999  3 4    .0811204
      4 1999  5 4   .09235437
      4 1999 16 4    .0219163
      4 1999  9 4   .11264074
      4 1999  1 4   .25785503
      4 1999 10 4   .05511673
      4 2000  6 5   .04661385
      4 2000 24 5           1
      4 2000  9 5   .15333287
      4 2000  7 5   .04258034
      4 2000  1 5    .1738673
      4 2000 10 5   .04822536
      4 2000  5 5   .11699335
      4 2000  3 5   .11039157
      4 2000 13 5   .10598524
      4 2000 16 5  .019814735
      4 2000 22 5   .04728615
      4 2000  2 5  .031671267
      4 2000 12 5   .10323796
      4 2001 10 6   .04284507
      4 2001 22 6    .0454179
      4 2001  2 6    .0188323
      4 2001  6 6  .023150554
      4 2001  9 6   .09775935
      4 2001 16 6   .08947137
      4 2001  1 6   .15057746
      4 2001  7 6   .05812356
      4 2001 13 6    .1505476
      4 2001 12 6   .13022423
      4 2001  3 6   .09104147
      4 2001  5 6   .10200913
      4 2001 24 6           1
      4 2002  3 7   .11352996
      4 2002 13 7    .1539421
      4 2002  5 7   .10073135
      4 2002 10 7    .0654113
      4 2002  9 7   .09481904
      4 2002  2 7  .011311985
      4 2002  1 7    .1900311
      4 2002 24 7           1
      4 2002  7 7    .0556372
      4 2002 16 7  .012422678
      4 2002  6 7  .021615803
      4 2002 22 7   .04022419
      4 2002 12 7    .1403233
      4 2003 22 8   .04990193
      4 2003  1 8   .18242712
      4 2003  6 8  .020329406
      4 2003  7 8  .031922758
      4 2003  3 8   .08527122
      4 2003  2 8   .00735881
      4 2003 16 8  .011391565
      4 2003  5 8   .08295328
      4 2003 10 8   .04215821
      4 2003 24 8           1
      4 2003 12 8   .16217557
      4 2003  9 8    .2027528
      4 2003 13 8   .12135734
      4 2004  1 9   .20530248
      end
      format %ty Time
      label values Country CTY1
      label def CTY1 4 "ALB", modify
      label values Industry ISIC1
      label def ISIC1 1 "15", modify
      label def ISIC1 2 "16", modify
      label def ISIC1 3 "17", modify
      label def ISIC1 5 "19", modify
      label def ISIC1 6 "20", modify
      label def ISIC1 7 "21", modify
      label def ISIC1 8 "22", modify
      label def ISIC1 9 "23", modify
      label def ISIC1 10 "24", modify
      label def ISIC1 12 "26", modify
      label def ISIC1 13 "27", modify
      label def ISIC1 16 "30", modify
      label def ISIC1 22 "36", modify
      label def ISIC1 24 "D", modify
      The code I used was mkmat ManShare, rownames(Industry), which would theoretically give me a vector which listed the proportion of manufacturing output of each industry over total output of a country in a particular year for all countries and years. However, I am trying to make a vector for each country and year pair, but am unable to do so because Stata does not allow bysort with mkmat.

      Comment


      • #4
        Here is one way. I name a matrix with its corresponding country label.

        Code:
        egen Indyr= group(Industry Time), label
        decode Indyr, gen(IY)
        levelsof Country, local(countries)
        clear matrix
        sort Country Industry Time
        foreach c of local countries{
            gen `=strtoname("`: lab `: val lab Country' `c''")' = ManShare
            mkmat `=strtoname("`: lab `: val lab Country' `c''")'  if Country==`c', rownames(IY)
            drop `=strtoname("`: lab `: val lab Country' `c''")'
        }
        mat dir
        Res.:

        Code:
        . mat dir
                  ALB[100,1]
        
        . mat l ALB
        
        ALB[100,1]
                       ALB
        15_1996  .42088395
        15_1997  .56261939
        15_1998  .21774356
        15_1999  .25785503
        15_2000   .1738673
        15_2001  .15057746
        15_2002   .1900311
        15_2003  .18242712
        15_2004  .20530248
        16_1996  .10667754
        16_1997   .0498813
        16_1998  .03051914
        16_1999  .02449911
        16_2000  .03167127
        16_2001   .0188323
        16_2002  .01131198
        16_2003  .00735881
        17_1996  .00042241
        17_1997  .00775417
        17_1998  .09556854
        17_1999   .0811204
        17_2000  .11039157
        17_2001  .09104147
        17_2002  .11352996
        17_2003  .08527122
        19_1998  .11324187
        19_1999  .09235437
        19_2000  .11699335
        19_2001  .10200913
        19_2002  .10073135
        19_2003  .08295328
        20_1996  .00514624
        20_1998  .07812565
        20_1999  .03600893
        20_2000  .04661385
        20_2001  .02315055
        20_2002   .0216158
        20_2003  .02032941
        21_1998  .05883621
        21_1999   .0550415
        21_2000  .04258034
        21_2001  .05812356
        21_2002   .0556372
        21_2003  .03192276
        22_1996  .00204699
        22_1997  .00285793
        23_1996  .18636119
        23_1997   .1573661
        23_1998  .15857527
        23_1999  .11264074
        23_2000  .15333287
        23_2001  .09775935
        23_2002  .09481904
        23_2003   .2027528
        24_1996  .00509373
        24_1997  .00915482
        24_1998  .02843487
        24_1999  .05511673
        24_2000  .04822536
        24_2001  .04284507
        24_2002   .0654113
        24_2003  .04215821
        26_1996  .06771848
        26_1997  .07125758
        26_1998  .08016227
        26_1999   .0996013
        26_2000  .10323796
        26_2001  .13022423
        26_2002   .1403233
        26_2003  .16217557
        27_1996  .15671143
        27_1997  .11123684
        27_1998   .1125359
        27_1999  .11118634
        27_2000  .10598524
        27_2001  .15054759
        27_2002  .15394209
        27_2003  .12135734
        30_1998   .0227555
        30_1999   .0219163
        30_2000  .01981474
        30_2001  .08947137
        30_2002  .01242268
        30_2003  .01139157
        36_1996  .00315916
        36_1997  .00042504
        36_1998  .00350122
        36_1999   .0526593
        36_2000  .04728615
        36_2001   .0454179
        36_2002  .04022419
        36_2003  .04990193
         D_1996          1
         D_1997          1
         D_1998          1
         D_1999          1
         D_2000          1
         D_2001          1
         D_2002          1
         D_2003          1
        Last edited by Andrew Musau; 14 Mar 2021, 05:50.

        Comment

        Working...
        X