Announcement

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

  • Summing across variables

    I have data with 160 countries and 6 sectors as variable names. I tried to use the foreach command to sum the six sectors of each country so I get 160 countries using

    foreach var of varlist AFG1-ZWE6 {
    egen `var'_FD = rowtotal (`var'*)
    }

    But this command just repeats the same variables and does not sum all the six sectors for each country. Could someone please help write the command well? Thanks in advance

  • #2
    I assume your data looks as below. However, if you insert part of your data that would help us better understand.

    Code:
    clear
    input str11 Country float(sector1 sector2 sector3 sector4 sector5 sector6)
    "Country 1"   10 20 30 40 50 60
    "Country 2"   10 20 30 40 50 60
    "Country 3"   10 20 30 40 50 60
    "Country 4"   10 20 30 40 50 60
    "Country 5"   10 20 30 40 50 60
    "Country 6"   10 20 30 40 50 60
    "Country 160" 10 20 30 40 50 60
    end
    If you want to sum values of all six sectors of each country, then you can easily do it using egen command as below:

    Code:
    egen total=rowtotal(sector*)
    
    list Country total
    
         +---------------------+
         |     Country   total |
         |---------------------|
      1. |   Country 1     210 |
      2. |   Country 2     210 |
      3. |   Country 3     210 |
      4. |   Country 4     210 |
      5. |   Country 5     210 |
         |---------------------|
      6. |   Country 6     210 |
      7. | Country 160     210 |
         +---------------------+






    Comment


    • #3
      Executive summary: This is a side-effect of an awkward data layout. The real problem is a need to reshape long.

      Marin: Your punishment is that you got what you asked for.

      Code:
      AFG1*
      is in your dataset just

      Code:
      AFG1
      and is not at all the same as

      Code:
      AFG*
      which is what you are reaching for.

      Fahim in #2 doesn't use the information in #1. My reading is that what is wanted is the sum of AFG1-AFG6, ...., the sum of ZWE1-ZWE6.

      That can be done but the bigger deal is that your data layout is wide and would for almost all Stata purposes be better off as long.

      Let's imagine getting the stubs AFG to ZWE.

      Code:
      unab all : AFG1-ZWE6
      
      forval j = 1/6 {
            local all : subinstr local all "`j'" "", all
      }
      
      list all : list uniq all
      Now you can go

      Code:
      foreach stub of local all {
            egen `stub_FD = rowtotal(`stub'?)
      }
      -- except that -- don't do this! You already have at least 960 variables. Now you are adding another 160 variables? What is your plan for working with all of these?

      My guess is that you have observations for different years and then countries and sectors define the variables. Show us the result of

      Code:
      dataex AFG1-AFG6
      and tell us about the variables in your dataset other than those you have mentioned

      Code:
      ds AFG1-ZWE6, not
      For explanation of dataex read again please FAQ Advice #12.

      Note. It is possible that this would work:

      Code:
      unab all: ???1
      local all : subinstr local all "1" "", all 
      but I can't be certain without seeing the names of the other variables.
      Last edited by Nick Cox; 14 Aug 2018, 00:55.

      Comment


      • #4
        Thank you all for the responses. Nick My data is input-output table with the same dimension. So if I make it long I will have the same variables as columns again. Unless I bilaterized the data with column id and row id but I am finding it difficult to do that. I tried the command you wrote you it didn't seem to work out.

        Comment


        • #5
          Input-output I would still make long layout as some variation on

          source, sink, what, when, how much

          I have used reshape a fair amount but I can't give precise suggestions without more detail.

          Comment


          • #6
            input float(AFG27 AFG28 AFG29 AFG30 AFG31 AFG32)
            210170 2082.2 147.53 1404.5 -2001.9 14.16
            9781.4 62.345 105.09 9.7002 345.56 10.253
            14536 29.149 115.92 413.64 3432.5 10.852
            1266400 17762 2.272 9.5438 6163.3 10.096
            363970 5874.7 18.39 11007 1019.9 34.924
            106750 1240.1 600.09 2654.4 1202.5 14.066
            645510 13202 1837.7 5518.1 -8413.2 21.631
            22967 396.04 655.28 21692 3739.5 59.847
            422260 4127.4 41057 617710 9526.3 1455.9
            368230 6776.7 28169 226570 15658 535.84
            209880 4495.7 6522.5 53408 1628 134.06
            94755 2904.4 568.41 13293 4531.7 42.224
            280940 4332.7 2.1878 9.4514 50.133 10.004
            9056.4 273.24 80354 1360400 48 3126.9
            64814 1351.2 319.95 5914.5 113.11 23.596
            376630 7833.9 13009 163490 4276.7 415.05
            1532400 34616 -3280.1 10253 -228.43 50.457
            450720 3562.3 -4697.7 -80085 -228.43 -162.46
            -438250 -21273 -9712.2 -119500 277.38 -244.96
            465020 9115.3 3151.4 94934 90.686 230.23
            3456000 63454 27127 277430 8653.1 642.35
            59334 1323.8 1424300 555700 47.996 1278.8
            2525800 56895 683380 85686 54.71 205.53
            52153 1490.4 888.12 9.4688 47.99 10.021
            53075 1503.9 2.1904 9.462 47.988 10.014
            2.3254 2.5338 3.5612 9.8101 2.9921 9.9908
            3.6257 3.5557 1.5039 9.34 6.0615 9.34
            2.0505 2.0356 .92997 8.3339 4.9325 8.3339
            3.3056 3.2401 1.6271 10.011 7.7325 10.011
            3.6517 3.5505 1.6154 9.7563 7.6761 9.7563
            3.7245 3.5765 1.6539 10.055 8.1718 10.055
            3.7968 3.684 1.5316 9.6572 7.4572 9.6572
            3.4543 3.3605 1.4848 9.5506 7.3809 9.5506
            80.539 35.3 2.2822 25.908 8.0339 25.908
            3.4465 3.3527 1.4138 9.3288 7.1431 9.3288
            2.4448 2.3968 1.3103 9.0183 6.5947 9.0183
            3.0181 2.9512 1.2737 8.9634 6.3206 8.9634
            4.1799 4.0214 1.8473 10.593 8.8151 10.593
            2.4807 2.4292 1.4135 9.2312 6.398 9.2312
            4.1453 4.0028 1.7685 10.221 8.1957 10.221
            4.1231 3.9806 1.7012 10.11 8.1089 10.11
            2.724 2.511 -4.5421 8.5891 -60.191 8.5891
            2.7567 2.5437 -4.484 8.5921 -60.152 8.5921
            3.0613 2.702 -4.4106 8.8074 -60.075 8.8074
            3.2297 3.1421 -14.613 9.2209 -126.74 9.2209
            4.1759 4.0327 1.6776 10.183 8.1121 10.183
            1.5647 1.5646 .25843 7.5198 4.4768 7.5198
            3.8056 3.6631 1.4376 9.7452 7.8791 9.7452
            3.8424 3.6946 1.5301 9.828 7.9668 9.828
            3.9673 3.8248 1.6293 9.9248 8.0228 9.9248
            1.5698 1.5697 .26939 7.5313 4.4852 7.5313
            3.5574 3.4149 1.5611 9.7347 7.9435 9.7347
            2.6015 2.5211 1.2962 6.9381 6.4353 6.9381
            1.9452 1.9098 1.1552 6.4567 5.5485 6.4567
            2.3348 2.2428 1.3459 6.9986 6.8519 6.9986
            2.6379 2.5442 1.3822 7.1738 6.9335 7.1738
            2.3141 2.2436 1.1651 6.5281 5.963 6.5281
            1.7869 1.7539 .91965 5.9908 5.0482 5.9908
            2.5195 2.419 1.3454 6.9934 6.8506 6.9934
            2.5689 2.4718 1.3613 6.9975 6.8581 6.9975
            2.4307 2.3488 1.2727 6.8593 6.5938 6.8593
            2.0607 2.0001 1.2775 6.8436 6.6962 6.8436
            1.263 1.2629 .5357 5.5204 4.7538 5.5204
            2.6965 2.5626 1.4968 7.6958 7.7306 7.6958
            1.1301 1.1299 .34206 5.3651 4.6509 5.3651
            2.5245 2.4266 1.3264 6.949 6.822 6.949
            2.6404 2.5425 1.4019 7.1164 6.8875 7.1164
            1.5522 1.3882 -2.9783 5.9198 -41.404 5.9198
            1.6046 1.4406 -2.9288 6.0449 -41.334 6.0449
            1.5667 1.4027 -2.9436 5.9946 -41.367 5.9946
            1.5121 1.3542 -10.376 5.8981 -89.063 5.8981
            2.7179 2.6199 1.3715 7.1447 6.8761 7.1447
            1.124 1.1239 .32783 5.3523 4.6428 5.3523
            2.495 2.397 1.2775 6.9047 6.7737 6.9047
            2.4662 2.3683 1.3327 6.9443 6.8165 6.9443
            2.646 2.5481 1.4127 7.1425 6.901 7.1425
            1.129 1.1289 .34131 5.3651 4.6517 5.3651
            2.0747 1.9768 .91961 6.4372 6.4863 6.4372
            1.0998 1.0997 .40365 4.9449 4.4841 4.9449
            1.0642 1.0641 .35691 4.9353 4.4939 4.9353
            1.0552 1.0551 .34492 4.9186 4.4818 4.9186
            1.8618 1.8261 1.0149 5.9644 5.0115 5.9644
            1.3264 1.3263 .48807 5.1198 4.6006 5.1198
            1.6187 1.6153 .70139 5.4254 4.7882 5.4254
            2.4921 2.4218 1.1646 6.3585 6.0617 6.3585
            2.1278 2.0799 1.065 6.1302 5.6205 6.1302
            2.0157 1.9714 .96676 5.8719 5.0964 5.8719
            1.9138 1.8735 1.0549 6.0065 5.5219 6.0065
            1.5256 1.5254 .69057 5.4013 4.7813 5.4013
            1.6992 1.6809 .86388 5.6167 4.9182 5.6167
            1.051 1.0509 .33923 4.9112 4.4762 4.9112
            2.8064 2.7169 1.2921 6.6872 6.394 6.6872
            2.8574 2.7679 1.3349 6.7577 6.4356 6.7577
            1.8667 1.7302 -2.6534 5.6801 -37.706 5.6801
            1.9645 1.828 -2.5958 5.7847 -37.657 5.7847
            1.9128 1.7763 -2.6209 5.7563 -37.663 5.7563
            1.9785 1.8493 -9.4235 5.7973 -82.054 5.7973
            2.7791 2.6896 1.2872 6.6645 6.3698 6.6645
            1.0446 1.0445 .32737 4.8973 4.4636 4.8973
            2.679 2.5895 1.2092 6.5738 6.3346 6.5738
            end
            [/CODE]

            Comment


            • #7
              This is a snapshot Click image for larger version

Name:	2018-08-14 (3).png
Views:	2
Size:	137.1 KB
ID:	1458068 Click image for larger version

Name:	2018-08-14 (3).png
Views:	2
Size:	137.1 KB
ID:	1458069

              Comment


              • #8
                Better to give us the results of

                Code:
                ds
                or the first hundred or so variables at least.

                Note: the code in #1 doesn't make full sense given what you are showing us. You don't have variables AFG1-AFG6 so far as I can see.

                Comment


                • #9
                  I have AFG27-AFG32, which is similar to AFG1-AFG6.

                  Comment


                  • #10
                    ds
                    countries CRI32 JOR28 PNG30 UGA32 Djibouti10 Rwanda10
                    countrycode HRV27 JOR29 PNG31 UKR27 Djibouti11 Rwanda11
                    Indust_Comm HRV28 JOR30 PNG32 UKR28 Djibouti12 Rwanda12
                    sector HRV29 JOR31 PRY27 UKR29 Djibouti15 Rwanda15
                    id HRV30 JOR32 PRY28 UKR30 Djibouti16 Rwanda16
                    year HRV31 KAZ27 PRY29 UKR31 Eritrea1 SaoTomean~e1
                    indus_id HRV32 KAZ28 PRY30 UKR32 Eritrea2 SaoTomean~e2
                    Countryid CUB27 KAZ29 PRY31 ARE27 Eritrea3 SaoTomeand~3
                    ROW1 CUB28 KAZ30 PRY32 ARE28 Eritrea4 SaoTomeand~4
                    AFG27 CUB29 KAZ31 PER27 ARE29 Eritrea5 SaoTomean~e5
                    AFG28 CUB30 KAZ32 PER28 ARE30 Eritrea6 SaoTomean~e6
                    AFG29 CUB31 KEN27 PER29 ARE31 Eritrea7 SaoTomeand~7
                    AFG30 CUB32 KEN28 PER30 ARE32 Eritrea8 SaoTomeand~8
                    AFG31 CYP27 KEN29 PER31 GBR27 Eritrea9 SaoTomeand~9
                    AFG32 CYP28 KEN30 PER32 GBR28 Eritrea10 SaoTomean~10
                    ALB27 CYP29 KEN31 PHL27 GBR29 Eritrea11 SaoTomean~11
                    ALB28 CYP30 KEN32 PHL28 GBR30 Eritrea12 SaoTomean~12
                    ALB29 CYP31 KWT27 PHL29 GBR31 Eritrea15 SaoTomean~15
                    ALB30 CYP32 KWT28 PHL30 GBR32 Eritrea16 SaoTomean~16
                    ALB31 CZE27 KWT29 PHL31 TZA27 Ethiopia1 Senegal1
                    ALB32 CZE28 KWT30 PHL32 TZA28 Ethiopia2 Senegal2
                    DZA27 CZE29 KWT31 POL27 TZA29 Ethiopia3 Senegal3
                    DZA28 CZE30 KWT32 POL28 TZA30 Ethiopia4 Senegal4
                    DZA29 CZE31 KGZ27 POL29 TZA31 Ethiopia5 Senegal6
                    DZA30 CZE32 KGZ28 POL30 TZA32 Ethiopia6 Senegal7
                    DZA31 CIV27 KGZ29 POL31 USA27 Ethiopia7 Senegal8
                    DZA32 CIV28 KGZ30 POL32 USA28 Ethiopia8 Senegal9
                    AND27 CIV29 KGZ31 PRT27 USA29 Ethiopia9 Senegal10
                    AND28 CIV30 KGZ32 PRT28 USA30 Ethiopia10 Senegal11
                    AND29 CIV31 LAO27 PRT29 USA31 Ethiopia11 Senegal12
                    AND30 CIV32 LAO28 PRT30 USA32 Ethiopia12 Senegal15
                    AND31 PRK27 LAO29 PRT31 URY27 Ethiopia15 Senegal16
                    AND32 PRK28 LAO30 PRT32 URY28 Ethiopia16 Seychelles1
                    AGO27 PRK29 LAO31 QAT27 URY29 Gabon1 Seychelles2
                    AGO28 PRK30 LAO32 QAT28 URY30 Gabon2 Seychelles3
                    AGO29 PRK31 LVA27 QAT29 URY31 Gabon3 Seychelles4
                    AGO30 PRK32 LVA28 QAT30 URY32 Gabon4 Seychelles5
                    AGO31 COD27 LVA29 QAT31 UZB27 Gabon5 Seychelles6
                    AGO32 COD28 LVA30 QAT32 UZB28 Gabon6 Seychelles7
                    ATG27 COD29 LVA31 KOR27 UZB29 Gabon7 Seychelles8
                    ATG28 COD30 LVA32 KOR28 UZB30 Gabon8 Seychelles9
                    ATG29 COD31 LBN27 KOR29 UZB31 Gabon9 Seychelles10
                    ATG30 COD32 LBN28 KOR30 UZB32 Gabon10 Seychelles11
                    ATG31 DNK27 LBN29 KOR31 VUT27 Gabon11 Seychelles12
                    ATG32 DNK28 LBN30 KOR32 VUT28 Gabon12 Seychelles15
                    ARG27 DNK29 LBN31 MDA27 VUT29 Gabon15 Seychelles16
                    ARG28 DNK30 LBN32 MDA28 VUT30 Gabon16 SierraLeone1
                    ARG29 DNK31 LSO27 MDA29 VUT31 Gambia1 SierraLeone2
                    ARG30 DNK32 LSO28 MDA30 VUT32 Gambia2 SierraLeone3
                    ARG31 DJI27 LSO29 MDA31 VEN27 Gambia3 SierraLeone4
                    ARG32 DJI28 LSO30 MDA32 VEN28 Gambia4 SierraLeone5
                    ARM27 DJI29 LSO31 ROU27 VEN29 Gambia5 SierraLeone6
                    ARM28 DJI30 LSO32 ROU28 VEN30 Gambia6 SierraLeone7
                    ARM29 DJI31 LBR27 ROU29 VEN31 Gambia7 SierraLeone8
                    ARM30 DJI32 LBR28 ROU30 VEN32 Gambia8 SierraLeone9
                    ARM31 DOM27 LBR29 ROU31 VNM27 Gambia9 SierraLeo~10
                    ARM32 DOM28 LBR30 ROU32 VNM28 Gambia10 SierraLeo~11
                    ABW27 DOM29 LBR31 RUS27 VNM29 Gambia11 SierraLeo~12
                    ABW28 DOM30 LBR32 RUS28 VNM30 Gambia12 SierraLeo~15
                    ABW29 DOM31 LBY27 RUS29 VNM31 Gambia15 SierraLeo~16
                    ABW30 DOM32 LBY28 RUS30 VNM32 Gambia16 Somalia1
                    ABW31 ECU27 LBY29 RUS31 YEM27 Ghana1 Somalia2
                    ABW32 ECU28 LBY30 RUS32 YEM28 Ghana2 Somalia3
                    AUS27 ECU29 LBY31 RWA27 YEM29 Ghana3 Somalia4
                    AUS28 ECU30 LBY32 RWA28 YEM30 Ghana4 Somalia5
                    AUS29 ECU31 LIE27 RWA29 YEM31 Ghana5 Somalia6
                    AUS30 ECU32 LIE28 RWA30 YEM32 Ghana6 Somalia7
                    AUS31 EGY27 LIE29 RWA31 ZMB27 Ghana7 Somalia8
                    AUS32 EGY28 LIE30 RWA32 ZMB28 Ghana8 Somalia9
                    AUT27 EGY29 LIE31 WSM27 ZMB29 Ghana9 Somalia10
                    AUT28 EGY30 LIE32 WSM28 ZMB30 Ghana10 Somalia11
                    AUT29 EGY31 LTU27 WSM29 ZMB31 Ghana11 Somalia12
                    AUT30 EGY32 LTU28 WSM30 ZMB32 Ghana12 Somalia15
                    AUT31 SLV27 LTU29 WSM31 ZWE27 Ghana15 Somalia16
                    AUT32 SLV28 LTU30 WSM32 ZWE28 Ghana16 SouthSudan1
                    AZE27 SLV29 LTU31 SMR27 ZWE29 Guinea1 SouthSudan2
                    AZE28 SLV30 LTU32 SMR28 ZWE30 Guinea2 SouthSudan3
                    AZE29 SLV31 LUX27 SMR29 ZWE31 Guinea3 SouthSudan4
                    AZE30 SLV32 LUX28 SMR30 ZWE32 Guinea4 SouthSudan5
                    AZE31 ERI27 LUX29 SMR31 Angola1 Guinea5 SouthSudan6
                    AZE32 ERI28 LUX30 SMR32 Angola2 Guinea6 SouthSudan7
                    BHS27 ERI29 LUX31 STP27 Angola3 Guinea7 SouthSudan8
                    BHS28 ERI30 LUX32 STP28 Angola4 Guinea8 SouthSudan9
                    BHS29 ERI31 MAC27 STP29 Angola5 Guinea9 SouthSudan10
                    BHS30 ERI32 MAC28 STP30 Angola6 Guinea10 SouthSudan11
                    BHS31 EST27 MAC29 STP31 Angola7 Guinea11 SouthSudan12
                    BHS32 EST28 MAC30 STP32 Angola8 Guinea12 SouthSudan15
                    BHR27 EST29 MAC31 SAU27 Angola9 Guinea15 SouthSudan16
                    BHR28 EST30 MAC32 SAU28 Angola10 Guinea16 Sudan1
                    BHR29 EST31 MDG27 SAU29 Angola11 Kenya1 Sudan2
                    BHR30 EST32 MDG28 SAU30 Angola12 Kenya2 Sudan3
                    BHR31 ETH27 MDG29 SAU31 Angola13 Kenya3 Sudan4
                    BHR32 ETH28 MDG30 SAU32 Angola14 Kenya4 Sudan5
                    BGD27 ETH29 MDG31 SEN27 Angola15 Kenya5 Sudan6
                    BGD28 ETH30 MDG32 SEN28 Angola16 Kenya6 Sudan7
                    BGD29 ETH31 MWI27 SEN29 Benin1 Kenya7 Sudan8
                    BGD30 ETH32 MWI28 SEN30 Benin2 Kenya8 Sudan9
                    BGD31 FJI27 MWI29 SEN31 Benin3 Kenya9 Sudan10
                    BGD32 FJI28 MWI30 SEN32 Benin4 Kenya10 Sudan11
                    BRB27 FJI29 MWI31 SRB27 Benin5 Kenya11 Sudan12
                    BRB28 FJI30 MWI32 SRB28 Benin6 Kenya12 Sudan15
                    BRB29 FJI31 MYS27 SRB29 Benin7 Kenya15 Sudan16
                    BRB30 FJI32 MYS28 SRB30 Benin8 Kenya16 Swaziland1
                    BRB31 FIN27 MYS29 SRB31 Benin9 Lesotho1 Swaziland2
                    BRB32 FIN28 MYS30 SRB32 Benin10 Lesotho2 Swaziland3
                    BLR27 FIN29 MYS31 SYC27 Benin11 Lesotho3 Swaziland4
                    BLR28 FIN30 MYS32 SYC28 Benin12 Lesotho4 Swaziland5
                    BLR29 FIN31 MDV27 SYC29 Benin13 Lesotho5 Swaziland6
                    BLR30 FIN32 MDV28 SYC30 Benin14 Lesotho6 Swaziland7
                    BLR31 FRA27 MDV29 SYC31 Benin15 Lesotho7 Swaziland8
                    BLR32 FRA28 MDV30 SYC32 Benin16 Lesotho8 Swaziland9
                    BEL27 FRA29 MDV31 SLE27 Botswana1 Lesotho9 Swaziland10
                    BEL28 FRA30 MDV32 SLE28 Botswana2 Lesotho10 Swaziland11
                    BEL29 FRA31 MLI27 SLE29 Botswana3 Lesotho11 Swaziland12
                    BEL30 FRA32 MLI28 SLE30 Botswana4 Lesotho12 Swaziland15
                    BEL31 PYF27 MLI29 SLE31 Botswana5 Lesotho15 Swaziland16
                    BEL32 PYF28 MLI30 SLE32 Botswana6 Lesotho16 Togo1
                    BLZ27 PYF29 MLI31 SGP27 Botswana7 Liberia1 Togo2
                    BLZ28 PYF30 MLI32 SGP28 Botswana8 Liberia2 Togo3
                    BLZ29 PYF31 MLT27 SGP29 Botswana9 Liberia3 Togo4
                    BLZ30 PYF32 MLT28 SGP30 Botswana10 Liberia4 Togo5
                    BLZ31 GAB27 MLT29 SGP31 Botswana11 Liberia5 Togo6
                    BLZ32 GAB28 MLT30 SGP32 Botswana12 Liberia6 Togo7
                    BEN27 GAB29 MLT31 SVK27 Botswana13 Liberia7 Togo8
                    BEN28 GAB30 MLT32 SVK28 Botswana14 Liberia8 Togo9
                    BEN29 GAB31 MRT27 SVK29 Botswana15 Liberia9 Togo10
                    BEN30 GAB32 MRT28 SVK30 Botswana16 Liberia10 Togo11
                    BEN31 GMB27 MRT29 SVK31 Burkina1 Liberia11 Togo12
                    BEN32 GMB28 MRT30 SVK32 Burkina2 Liberia12 Togo15
                    BMU27 GMB29 MRT31 SVN27 Burkina3 Liberia15 Togo16
                    BMU28 GMB30 MRT32 SVN28 Burkina4 Liberia16 Uganda1
                    BMU29 GMB31 MUS27 SVN29 Burkina5 Madagascar1 Uganda2
                    BMU30 GMB32 MUS28 SVN30 Burkina6 Madagascar2 Uganda3
                    BMU31 GEO27 MUS29 SVN31 Burkina7 Madagascar3 Uganda4
                    BMU32 GEO28 MUS30 SVN32 Burkina8 Madagascar4 Uganda5
                    BTN27 GEO29 MUS31 SOM27 Burkina9 Madagascar5 Uganda6
                    BTN28 GEO30 MUS32 SOM28 Burkina10 Madagascar6 Uganda7
                    BTN29 GEO31 MEX27 SOM29 Burkina11 Madagascar7 Uganda8
                    BTN30 GEO32 MEX28 SOM30 Burkina12 Madagascar8 Uganda9
                    BTN31 DEU27 MEX29 SOM31 Burkina13 Madagascar9 Uganda10
                    BTN32 DEU28 MEX30 SOM32 Burkina14 Madagascar10 Uganda11
                    BOL27 DEU29 MEX31 ZAF27 Burkina15 Madagascar11 Uganda12
                    BOL28 DEU30 MEX32 ZAF28 Burkina16 Madagascar12 Uganda15
                    BOL29 DEU31 MCO27 ZAF29 Burundi1 Madagascar15 Uganda16
                    BOL30 DEU32 MCO28 ZAF30 Burundi2 Madagascar16 Tanzania1
                    BOL31 GHA27 MCO29 ZAF31 Burundi3 Malawi1 Tanzania2
                    BOL32 GHA28 MCO30 ZAF32 Burundi4 Malawi2 Tanzania3
                    BIH27 GHA29 MCO31 SDS27 Burundi5 Malawi3 Tanzania4
                    BIH28 GHA30 MCO32 SDS28 Burundi6 Malawi4 Tanzania5
                    BIH29 GHA31 MNG27 SDS29 Burundi7 Malawi5 Tanzania6
                    BIH30 GHA32 MNG28 SDS30 Burundi8 Malawi6 Tanzania7
                    BIH31 GRC27 MNG29 SDS31 Burundi9 Malawi7 Tanzania8
                    BIH32 GRC28 MNG30 SDS32 Burundi10 Malawi8 Tanzania9
                    BWA27 GRC29 MNG31 ESP27 Burundi11 Malawi9 Tanzania10
                    BWA28 GRC30 MNG32 ESP28 Burundi12 Malawi10 Tanzania11
                    BWA29 GRC31 MNE27 ESP29 Burundi13 Malawi11 Tanzania12
                    BWA30 GRC32 MNE28 ESP30 Burundi14 Malawi12 Tanzania15
                    BWA31 GRL27 MNE29 ESP31 Burundi15 Malawi15 Tanzania16
                    BWA32 GRL28 MNE30 ESP32 Burundi16 Malawi16 Zambia1
                    BRA27 GRL29 MNE31 LKA27 Cameroon1 Mali1 Zambia2
                    BRA28 GRL30 MNE32 LKA28 Cameroon2 Mali2 Zambia3
                    BRA29 GRL31 MAR27 LKA29 Cameroon3 Mali3 Zambia4
                    BRA30 GRL32 MAR28 LKA30 Cameroon4 Mali4 Zambia5
                    BRA31 GTM27 MAR29 LKA31 Cameroon5 Mali5 Zambia6
                    BRA32 GTM28 MAR30 LKA32 Cameroon6 Mali6 Zambia7
                    VGB27 GTM29 MAR31 SUD27 Cameroon7 Mali7 Zambia8
                    VGB28 GTM30 MAR32 SUD28 Cameroon8 Mali8 Zambia9
                    VGB29 GTM31 MOZ27 SUD29 Cameroon9 Mali9 Zambia10
                    VGB30 GTM32 MOZ28 SUD30 Cameroon10 Mali10 Zambia11
                    VGB31 GIN27 MOZ29 SUD31 Cameroon11 Mali11 Zambia12
                    VGB32 GIN28 MOZ30 SUD32 Cameroon12 Mali12 Zambia15
                    BRN27 GIN29 MOZ31 SUR27 Cameroon15 Mali15 Zambia16
                    BRN28 GIN30 MOZ32 SUR28 Cameroon16 Mali16 Zimbabwe1
                    BRN29 GIN31 MMR27 SUR29 CapeVerde1 Mauritania1 Zimbabwe2
                    BRN30 GIN32 MMR28 SUR30 CapeVerde2 Mauritania2 Zimbabwe3
                    BRN31 GUY27 MMR29 SUR31 CapeVerde3 Mauritania3 Zimbabwe4
                    BRN32 GUY28 MMR30 SUR32 CapeVerde4 Mauritania4 Zimbabwe5
                    BGR27 GUY29 MMR31 SWZ27 CapeVerde5 Mauritania5 Zimbabwe6
                    BGR28 GUY30 MMR32 SWZ28 CapeVerde6 Mauritania6 Zimbabwe7
                    BGR29 GUY31 NAM27 SWZ29 CapeVerde7 Mauritania7 Zimbabwe8
                    BGR30 GUY32 NAM28 SWZ30 CapeVerde8 Mauritania8 Zimbabwe9
                    BGR31 HTI27 NAM29 SWZ31 CapeVerde9 Mauritania9 Zimbabwe10
                    BGR32 HTI28 NAM30 SWZ32 CapeVerde10 Mauritania10 Zimbabwe11
                    BFA27 HTI29 NAM31 SWE27 CapeVerde11 Mauritania11 Zimbabwe12
                    BFA28 HTI30 NAM32 SWE28 CapeVerde12 Mauritania12 Zimbabwe15
                    BFA29 HTI31 NPL27 SWE29 CapeVerde15 Mauritania15 Zimbabwe16
                    BFA30 HTI32 NPL28 SWE30 CapeVerde16 Mauritania16 otherafrica1
                    BFA31 HND27 NPL29 SWE31 CentralAf~p1 Mauritius1 otherafrica2
                    BFA32 HND28 NPL30 SWE32 CentralAf~p2 Mauritius2 otherafrica3
                    BDI27 HND29 NPL31 CHE27 CentralAfr~3 Mauritius3 otherafrica4
                    BDI28 HND30 NPL32 CHE28 CentralAfr~4 Mauritius4 otherafrica5
                    BDI29 HND31 NLD27 CHE29 CentralAf~p5 Mauritius5 otherafrica6
                    BDI30 HND32 NLD28 CHE30 CentralAf~p6 Mauritius6 otherafrica7
                    BDI31 HKG27 NLD29 CHE31 CentralAfr~7 Mauritius7 otherafrica8
                    BDI32 HKG28 NLD30 CHE32 CentralAfr~8 Mauritius8 otherafrica9
                    KHM27 HKG29 NLD31 SYR27 CentralAfr~9 Mauritius9 otherafri~10
                    KHM28 HKG30 NLD32 SYR28 CentralAf~10 Mauritius10 otherafri~11
                    KHM29 HKG31 ANT27 SYR29 CentralAf~11 Mauritius11 otherafri~12
                    KHM30 HKG32 ANT28 SYR30 CentralAf~12 Mauritius12 otherafri~13
                    KHM31 HUN27 ANT29 SYR31 CentralAf~15 Mauritius15 otherafri~14
                    KHM32 HUN28 ANT30 SYR32 CentralAf~16 Mauritius16 otherafri~15
                    CMR27 HUN29 ANT31 TWN27 Chad1 Mozambique1 otherafri~16
                    CMR28 HUN30 ANT32 TWN28 Chad2 Mozambique2 EU1
                    CMR29 HUN31 NCL27 TWN29 Chad3 Mozambique3 EU2
                    CMR30 HUN32 NCL28 TWN30 Chad4 Mozambique4 EU3
                    CMR31 ISL27 NCL29 TWN31 Chad5 Mozambique5 EU4
                    CMR32 ISL28 NCL30 TWN32 Chad6 Mozambique6 EU5
                    CAN27 ISL29 NCL31 TJK27 Chad7 Mozambique7 EU6
                    CAN28 ISL30 NCL32 TJK28 Chad8 Mozambique8 EU7
                    CAN29 ISL31 NZL27 TJK29 Chad9 Mozambique9 EU8
                    CAN30 ISL32 NZL28 TJK30 Chad10 Mozambique10 EU9
                    CAN31 IND27 NZL29 TJK31 Chad11 Mozambique11 EU10
                    CAN32 IND28 NZL30 TJK32 Chad12 Mozambique12 EU11
                    CPV27 IND29 NZL31 THA27 Chad15 Mozambique15 EU12
                    CPV28 IND30 NZL32 THA28 Chad16 Mozambique16 EU13
                    CPV29 IND31 NIC27 THA29 Congo1 Namibia1 EU14
                    CPV30 IND32 NIC28 THA30 Congo2 Namibia2 EU15
                    CPV31 IDN27 NIC29 THA31 Congo3 Namibia3 EU16
                    CPV32 IDN28 NIC30 THA32 Congo4 Namibia4 USAA1
                    CYM27 IDN29 NIC31 MKD27 Congo5 Namibia5 USAA2
                    CYM28 IDN30 NIC32 MKD28 Congo6 Namibia6 USAA3
                    CYM29 IDN31 NER27 MKD29 Congo7 Namibia7 USAA4
                    CYM30 IDN32 NER28 MKD30 Congo8 Namibia8 USAA5
                    CYM31 IRN27 NER29 MKD31 Congo9 Namibia9 USAA6
                    CYM32 IRN28 NER30 MKD32 Congo10 Namibia10 USAA7
                    CAF27 IRN29 NER31 TGO27 Congo11 Namibia11 USAA8
                    CAF28 IRN30 NER32 TGO28 Congo12 Namibia12 USAA9
                    CAF29 IRN31 NGA27 TGO29 Congo15 Namibia15 USAA10
                    CAF30 IRN32 NGA28 TGO30 Congo16 Namibia16 USAA11
                    CAF31 IRQ27 NGA29 TGO31 CoteDIvoire1 Niger1 USAA12
                    CAF32 IRQ28 NGA30 TGO32 CoteDIvoire2 Niger2 USAA13
                    TCD27 IRQ29 NGA31 TTO27 CoteDIvoire3 Niger3 USAA14
                    TCD28 IRQ30 NGA32 TTO28 CoteDIvoire4 Niger4 USAA15
                    TCD29 IRQ31 NOR27 TTO29 CoteDIvoire5 Niger5 USAA16
                    TCD30 IRQ32 NOR28 TTO30 CoteDIvoire6 Niger6 China1
                    TCD31 IRL27 NOR29 TTO31 CoteDIvoire7 Niger7 China2
                    TCD32 IRL28 NOR30 TTO32 CoteDIvoire8 Niger8 China3
                    CHL27 IRL29 NOR31 TUN27 CoteDIvoire9 Niger9 China4
                    CHL28 IRL30 NOR32 TUN28 CoteDIvoi~10 Niger10 China5
                    CHL29 IRL31 PSE27 TUN29 CoteDIvoi~11 Niger11 China6
                    CHL30 IRL32 PSE28 TUN30 CoteDIvoi~12 Niger12 China7
                    CHL31 ISR27 PSE29 TUN31 CoteDIvoi~15 Niger15 China8
                    CHL32 ISR28 PSE30 TUN32 CoteDIvoi~16 Niger16 China9
                    CHN27 ISR29 PSE31 TUR27 DRCongo1 Nigeria1 China10
                    CHN28 ISR30 PSE32 TUR28 DRCongo2 Nigeria2 China11
                    CHN29 ISR31 OMN27 TUR29 DRCongo3 Nigeria3 China12
                    CHN30 ISR32 OMN28 TUR30 DRCongo4 Nigeria4 China13
                    CHN31 ITA27 OMN29 TUR31 DRCongo5 Nigeria5 China14
                    CHN32 ITA28 OMN30 TUR32 DRCongo6 Nigeria6 China15
                    COL27 ITA29 OMN31 TKM27 DRCongo7 Nigeria7 China16
                    COL28 ITA30 OMN32 TKM28 DRCongo8 Nigeria8 RoW1
                    COL29 ITA31 PAK27 TKM29 DRCongo9 Nigeria9 RoW2
                    COL30 ITA32 PAK28 TKM30 DRCongo10 Nigeria10 RoW3
                    COL31 JAM27 PAK29 TKM31 DRCongo11 Nigeria11 RoW4
                    COL32 JAM28 PAK30 TKM32 DRCongo12 Nigeria12 RoW5
                    COG27 JAM29 PAK31 USR27 DRCongo15 Nigeria15 RoW6
                    COG28 JAM30 PAK32 USR28 DRCongo16 Nigeria16 RoW7
                    COG29 JAM31 PAN27 USR29 Djibouti1 Rwanda1 RoW8
                    COG30 JAM32 PAN28 USR30 Djibouti2 Rwanda2 RoW9
                    COG31 JPN27 PAN29 USR31 Djibouti3 Rwanda3 RoW10
                    COG32 JPN28 PAN30 USR32 Djibouti4 Rwanda4 RoW11
                    CRI27 JPN29 PAN31 UGA27 Djibouti5 Rwanda5 RoW12
                    CRI28 JPN30 PAN32 UGA28 Djibouti6 Rwanda6 RoW13
                    CRI29 JPN31 PNG27 UGA29 Djibouti7 Rwanda7 RoW14
                    CRI30 JPN32 PNG28 UGA30 Djibouti8 Rwanda8 RoW15
                    CRI31 JOR27 PNG29 UGA31 Djibouti9 Rwanda9 RoW16

                    Comment


                    • #11
                      unab all : AFG27-ZWE32

                      . forval j = 27/32 {
                      2. local all : subinstr local all "`j'" "", all
                      3. }

                      . foreach stub of local all {
                      2. egen `stub'_FD = rowtotal(`stub'?)
                      3. }

                      **Error message i get for running #3

                      variable AFG? not found

                      Comment


                      • #12
                        That helps. Any of these string?

                        Code:
                        ds, has(type string)
                        Please do read about CODE delimiters. You were half-way there in #6. You need to type

                        CODE
                        stuff
                        /CODE

                        but the delimiters must be surrounded by [ ]. Or use # in the Editors. If I do that I get

                        Code:
                         
                        stuff

                        Comment


                        • #13
                          The error in #11 is that the code assumes one-digit suffixes. Yours are two-digit in that example. You would need AFG* or AFG??

                          Comment


                          • #14
                            Thank you Nick for your suggestions.

                            unab all : AFG27-ZWE32

                            forval j = 27/32 {
                            local all : subinstr local all "`j'" "", all
                            }

                            .
                            .
                            . foreach stub of local all {
                            egen `stub'_FD = rowtotal(`stub'??)
                            .
                            The command runs alright but defines AFG_FD then gives this error:

                            "variable AFG_FD already defined"

                            Then stops



                            Comment


                            • #15
                              running code in #12 gives

                              [ds, has(type string)]

                              countries countrycode Indust_Comm sector

                              Comment

                              Working...
                              X