Announcement

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

  • variable not found

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str3 countrycode str16(yr2001 yr2002 yr2003 yr2004 yr2005 yr2006 yr2007 yr2008 yr2009 yr2010)
    "AFG" ".."               "4055179566.34981" "4515558808.10994" "5226778808.89209" "6209137624.77181" "6971285594.68013" "9747879531.86311" "10109305182.952"  "12416161048.8412" "15856678596.148" 
    "ALB" "3922100793.5403"  "4348068242.19512" "5611496257.14231" "7184685781.51876" "8052073539.38058" "8896072918.9814"  "10677324144.4289" "12881353507.8539" "12044208085.864"  "11926922828.9911"
    "DZA" "54744712814.8676" "56760355865.0082" "67863828412.6882" "85332581188.6107" "103198223709.439" "117027307540.89"  "134977082623.78"  "171000699876.747" "137211035770.034" "161207270185.25" 
    "ASM" ".."               "512000000"        "524000000"        "509000000"        "500000000"        "493000000"        "518000000"        "560000000"        "675000000"        "573000000"       
    "AND" "1546926174.49664" "1755910031.99699" "2361726862.30248" "2894921777.99851" "3159905484.39249" "3456442102.622"   "3952600602.24473" "4085630584.44412" "3674409558.21061" "3449925738.86069"
    "AGO" "8936063723.20121" "15285594828.418"  "17812704825.1957" "23552047248.4301" "36970918699.2523" "52381006892.038"  "65266452081.386"  "88538610805.3214" "70307166934.3311" "81699556136.6643"
    "ATG" "800481481.481481" "814381481.481481" "856396296.296296" "919729629.62963"  "1022962962.96296" "1157662962.96296" "1312759259.25926" "1370070370.37037" "1228329629.62963" "1148700000"      
    "ARG" "268696750000"     "97724004251.8602" "127586973492.177" "164657930452.787" "198737095012.282" "232557260817.308" "287530508430.568" "361558037110.419" "332976484577.619" "423627422092.49" 
    "ARM" "2118467913.37873" "2376335048.39976" "2807061008.69084" "3576615240.41616" "4900469950.09033" "6384451606.1421"  "9206301700.39619" "11662040713.8753" "8647936747.98704" "9260284937.79781"
    "ABW" "1896648044.69274" "1962011173.18436" "2044134078.21229" "2254748603.35196" "2359776536.31285" "2469832402.23464" "2677653631.28492" "2843016759.77654" "2553631284.9162"  "2453631284.9162" 
    "AUS" "379083932596.329" "395342716617.832" "467390797903.32"  "614166310997.297" "695075176665.163" "747556154537.287" "853955405511.502" "1055127126230.98" "928042998085.702" "1147589183475.73"
    "AUT" "197508773215.323" "214394866675.24"  "262273631180.054" "301457562038.541" "316092273276.015" "336280064332.411" "389185571506.052" "432051935642.945" "401758735822.211" "392275107258.667"
    "AZE" "5707720390.8515"  "6235856819.58445" "7276753894.71541" "8680370408.05943" "13245716099.0057" "20982986344.3027" "33050343782.7759" "48852482960.0779" "44291490420.5026" "52909294791.9262"
    "BHS" "8317830000"       "8881160000"       "8870090000"       "9055290000"       "9836200000"       "10167250000"      "10618340000"      "10526000000"      "9981960000"       "10095760000"     
    "BHR" "8976196808.51064" "9593510638.29787" "11074813829.7872" "13150159574.4681" "15968723404.2553" "18504760638.2979" "21730000000"      "25710904255.3191" "22938218085.1064" "25713271276.5957"
    "BGD" "53991289844.3291" "54724081490.5102" "60158929188.2556" "65108544250.0425" "69442943089.4309" "71819083683.7403" "79611888213.148"  "91631278239.3237" "102477791472.39"  "115279077465.226"
    "BRB" "3054500000"       "3106500000"       "3209500000"       "3444500000"       "3819500000"       "4217450802.54938" "4674007452.49163" "4784925079.03312" "4465657314.64407" "4529928302.16747"
    "BLR" "12354820143.8849" "14594249022.8922" "17827791321.3067" "23144351851.8519" "30207567316.6202" "36954312354.3124" "45277399813.6067" "60763483146.0674" "50874078052.2735" "57222490768.7143"
    end
    The above is part of the dataset, I want to 1) count the gdp of yr2001-yr2010 that belong to country "BLX" separately; 2)Destring GDP value: NaN == .
    The following is my code:

    foreach v of varlist yr`*' {
    local yr`*': variable label `v'
    rename `v' yr`*'destring yr`*', replace ignore("NaN")
    bys countrycode: egen gdp`*' = total(yr`*') if countrycode == "BLX"
    replace yr`*' = gdp`*' if countrycode == "BLX"
    drop gdp`*'
    }
    duplicates drop
    save "GDP.dta", replace

    But it turns out that variable yr not found,
    so why the "yr"cannot be found?

  • #2
    For the destringing, consider this code instead:
    Code:
    forvalues x = 2001/2010 {
        destring yr`x', replace force
    }
    I am not sure what you mean by
    count the gdp of yr2001-yr2010 that belong to country "BLX" separately
    If you could explain that more clearly, perhaps we can find a solution for that too.

    Also, note that there is no country BLX in your example data.

    Comment


    • #3
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str3 countrycode str16(yr2001 yr2002 yr2003 yr2004 yr2005 yr2006 yr2007 yr2008 yr2009 yr2010)
      "AFG" ".."               "4055179566.34981" "4515558808.10994" "5226778808.89209" "6209137624.77181" "6971285594.68013" "9747879531.86311" "10109305182.952"  "12416161048.8412" "15856678596.148" 
      "ALB" "3922100793.5403"  "4348068242.19512" "5611496257.14231" "7184685781.51876" "8052073539.38058" "8896072918.9814"  "10677324144.4289" "12881353507.8539" "12044208085.864"  "11926922828.9911"
      "DZA" "54744712814.8676" "56760355865.0082" "67863828412.6882" "85332581188.6107" "103198223709.439" "117027307540.89"  "134977082623.78"  "171000699876.747" "137211035770.034" "161207270185.25" 
      "ASM" ".."               "512000000"        "524000000"        "509000000"        "500000000"        "493000000"        "518000000"        "560000000"        "675000000"        "573000000"       
      "AND" "1546926174.49664" "1755910031.99699" "2361726862.30248" "2894921777.99851" "3159905484.39249" "3456442102.622"   "3952600602.24473" "4085630584.44412" "3674409558.21061" "3449925738.86069"
      "AGO" "8936063723.20121" "15285594828.418"  "17812704825.1957" "23552047248.4301" "36970918699.2523" "52381006892.038"  "65266452081.386"  "88538610805.3214" "70307166934.3311" "81699556136.6643"
      "ATG" "800481481.481481" "814381481.481481" "856396296.296296" "919729629.62963"  "1022962962.96296" "1157662962.96296" "1312759259.25926" "1370070370.37037" "1228329629.62963" "1148700000"      
      "ARG" "268696750000"     "97724004251.8602" "127586973492.177" "164657930452.787" "198737095012.282" "232557260817.308" "287530508430.568" "361558037110.419" "332976484577.619" "423627422092.49" 
      "ARM" "2118467913.37873" "2376335048.39976" "2807061008.69084" "3576615240.41616" "4900469950.09033" "6384451606.1421"  "9206301700.39619" "11662040713.8753" "8647936747.98704" "9260284937.79781"
      "ABW" "1896648044.69274" "1962011173.18436" "2044134078.21229" "2254748603.35196" "2359776536.31285" "2469832402.23464" "2677653631.28492" "2843016759.77654" "2553631284.9162"  "2453631284.9162" 
      "AUS" "379083932596.329" "395342716617.832" "467390797903.32"  "614166310997.297" "695075176665.163" "747556154537.287" "853955405511.502" "1055127126230.98" "928042998085.702" "1147589183475.73"
      "AUT" "197508773215.323" "214394866675.24"  "262273631180.054" "301457562038.541" "316092273276.015" "336280064332.411" "389185571506.052" "432051935642.945" "401758735822.211" "392275107258.667"
      "AZE" "5707720390.8515"  "6235856819.58445" "7276753894.71541" "8680370408.05943" "13245716099.0057" "20982986344.3027" "33050343782.7759" "48852482960.0779" "44291490420.5026" "52909294791.9262"
      "BHS" "8317830000"       "8881160000"       "8870090000"       "9055290000"       "9836200000"       "10167250000"      "10618340000"      "10526000000"      "9981960000"       "10095760000"     
      "BHR" "8976196808.51064" "9593510638.29787" "11074813829.7872" "13150159574.4681" "15968723404.2553" "18504760638.2979" "21730000000"      "25710904255.3191" "22938218085.1064" "25713271276.5957"
      "BGD" "53991289844.3291" "54724081490.5102" "60158929188.2556" "65108544250.0425" "69442943089.4309" "71819083683.7403" "79611888213.148"  "91631278239.3237" "102477791472.39"  "115279077465.226"
      "BRB" "3054500000"       "3106500000"       "3209500000"       "3444500000"       "3819500000"       "4217450802.54938" "4674007452.49163" "4784925079.03312" "4465657314.64407" "4529928302.16747"
      "BLR" "12354820143.8849" "14594249022.8922" "17827791321.3067" "23144351851.8519" "30207567316.6202" "36954312354.3124" "45277399813.6067" "60763483146.0674" "50874078052.2735" "57222490768.7143"
      "BLX" "236746141604.37"  "258383599375.177" "318082528506.588" "369214712443.206" "385714762230.039" "408259840868.823" "470922156309.453" "517328087920.078" "483254171097.812" "481420882905.001"
      "BLZ" "871590369.32848"  "932292858.88771"  "990727709.549365" "1057653620.03465" "1111453823.62023" "1211148304.51368" "1283112549.99844" "1352588867.72777" "1320605471.63149" "1387925188.49004"
      "BEN" "3666222600.09828" "4194342849.46574" "5349258334.31914" "6190270756.13743" "6567654493.50831" "7034111651.4964"  "8169048690.05254" "9787735426.00897" "9738626993.27696" "9535344283.47295"
      end
      yr2001 represents year2001, and similarly, yr2002 represents year2002. The values of the differdnt columns are GDP of different countries in different years. While "BLX" appears more than once,so I want to count the different values of GDP that belong to "BLX‘’. I hope I have explained this question clearly. Thank you!

      Comment


      • #4
        There are some very strange ideas on Stata syntax in #1. Rather than explain them all, here is syntax that works for your data example.

        You don't ask for a reshape long but you surely need one.

        Code:
        reshape long yr , i(countrycode) j(year)
        rename yr GNP
        replace GNP = "." if GNP == ".."
        destring GNP, replace

        Comment


        • #5
          yr2001 represents year2001, and similarly, yr2002 represents year2002. The values of the differdnt columns are GDP of different countries in different years. While "BLX" appears more than once,so I want to count the different values of GDP that belong to "BLX‘’.
          I am afraid I am still lost. Perhaps you can construct a small example of the problem, and show exactly how you would like your data to look, once the problem is solved?

          Comment


          • #6
            Originally posted by Nick Cox View Post
            There are some very strange ideas on Stata syntax in #1. Rather than explain them all, here is syntax that works for your data example.

            You don't ask for a reshape long but you surely need one.

            Code:
            reshape long yr , i(countrycode) j(year)
            rename yr GNP
            replace GNP = "." if GNP == ".."
            destring GNP, replace
            so before that I have to make "countrycode" unique, right?

            Comment


            • #7
              Originally posted by Hemanshu Kumar View Post

              I am afraid I am still lost. Perhaps you can construct a small example of the problem, and show exactly how you would like your data to look, once the problem is solved?
              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input str3 countrycode str16(yr2001 yr2002 yr2003 yr2004 yr2005 yr2006 yr2007 yr2008 yr2009 yr2010)
              ""    ""                 ""                 ""                 ""                 ""                 ""                 ""                 ""                 ""                 ""                
              ""    ""                 ""                 ""                 ""                 ""                 ""                 ""                 ""                 ""                 ""                
              ""    ""                 ""                 ""                 ""                 ""                 ""                 ""                 ""                 ""                 ""                
              ""    ""                 ""                 ""                 ""                 ""                 ""                 ""                 ""                 ""                 ""                
              ""    ""                 ""                 ""                 ""                 ""                 ""                 ""                 ""                 ""                 ""                
              "ABW" "1896648044.69274" "1962011173.18436" "2044134078.21229" "2254748603.35196" "2359776536.31285" "2469832402.23464" "2677653631.28492" "2843016759.77654" "2553631284.9162"  "2453631284.9162" 
              "AFE" "258818573322.586" "264869812959.289" "352658503930.87"  "438833793854.258" "512211130671.035" "575921416503.73"  "661179291210.003" "708286928848.111" "719216656140.903" "860478348236.464"
              "AFG" ".."               "4055179566.34981" "4515558808.10994" "5226778808.89209" "6209137624.77181" "6971285594.68013" "9747879531.86311" "10109305182.952"  "12416161048.8412" "15856678596.148" 
              "AFW" "148012829262.279" "176938027370.015" "204644531317.323" "254092555393.797" "310557534109.235" "393305207803.064" "461791382953.671" "566480827442.145" "507043593426.246" "591595778367.133"
              "AGO" "8936063723.20121" "15285594828.418"  "17812704825.1957" "23552047248.4301" "36970918699.2523" "52381006892.038"  "65266452081.386"  "88538610805.3214" "70307166934.3311" "81699556136.6643"
              "ALB" "3922100793.5403"  "4348068242.19512" "5611496257.14231" "7184685781.51876" "8052073539.38058" "8896072918.9814"  "10677324144.4289" "12881353507.8539" "12044208085.864"  "11926922828.9911"
              "AND" "1546926174.49664" "1755910031.99699" "2361726862.30248" "2894921777.99851" "3159905484.39249" "3456442102.622"   "3952600602.24473" "4085630584.44412" "3674409558.21061" "3449925738.86069"
              "ARB" "798508872207.506" "803126104894.899" "887856220506.332" "1056250800483.71" "1297897997606.52" "1538296658886.73" "1790043618080.77" "2253740516075.54" "1978805212789.63" "2327010098167.46"
              "ARE" "103311640571.818" "109816201497.617" "124346358066.712" "147824370319.946" "180617467964.602" "222116541865.214" "257916133424.098" "315474615738.598" "253547358747.447" "289787452661.511"
              "ARG" "268696750000"     "97724004251.8602" "127586973492.177" "164657930452.787" "198737095012.282" "232557260817.308" "287530508430.568" "361558037110.419" "332976484577.619" "423627422092.49" 
              "ARM" "2118467913.37873" "2376335048.39976" "2807061008.69084" "3576615240.41616" "4900469950.09033" "6384451606.1421"  "9206301700.39619" "11662040713.8753" "8647936747.98704" "9260284937.79781"
              "ASM" ".."               "512000000"        "524000000"        "509000000"        "500000000"        "493000000"        "518000000"        "560000000"        "675000000"        "573000000"       
              "ATG" "800481481.481481" "814381481.481481" "856396296.296296" "919729629.62963"  "1022962962.96296" "1157662962.96296" "1312759259.25926" "1370070370.37037" "1228329629.62963" "1148700000"      
              "AUS" "379083932596.329" "395342716617.832" "467390797903.32"  "614166310997.297" "695075176665.163" "747556154537.287" "853955405511.502" "1055127126230.98" "928042998085.702" "1147589183475.73"
              "AUT" "197508773215.323" "214394866675.24"  "262273631180.054" "301457562038.541" "316092273276.015" "336280064332.411" "389185571506.052" "432051935642.945" "401758735822.211" "392275107258.667"
              "AZE" "5707720390.8515"  "6235856819.58445" "7276753894.71541" "8680370408.05943" "13245716099.0057" "20982986344.3027" "33050343782.7759" "48852482960.0779" "44291490420.5026" "52909294791.9262"
              "BDI" "876794723.068586" "825394490.159111" "784654423.620476" "915257323.3961"   "1117113045.65222" "1273375020.26862" "1356199364.85882" "1611835901.9063"  "1781455092.07114" "2032135246.50004"
              "BEN" "3666222600.09828" "4194342849.46574" "5349258334.31914" "6190270756.13743" "6567654493.50831" "7034111651.4964"  "8169048690.05254" "9787735426.00897" "9738626993.27696" "9535344283.47295"
              "BFA" "3190371050.45524" "3622350203.94595" "4740768355.22974" "5451688868.42974" "6146352742.03313" "6547420133.69942" "7625723123.54939" "9451436358.96861" "9450697335.53565" "10109618964.2848"
              "BGD" "53991289844.3291" "54724081490.5102" "60158929188.2556" "65108544250.0425" "69442943089.4309" "71819083683.7403" "79611888213.148"  "91631278239.3237" "102477791472.39"  "115279077465.226"
              "BGR" "14183497963.1071" "16402846413.0958" "21144983551.6823" "26157894736.8421" "29869283400.0381" "34379808888.6039" "44405101469.5591" "54438966419.8639" "52023504656.2878" "50682061053.2016"
              "BHR" "8976196808.51064" "9593510638.29787" "11074813829.7872" "13150159574.4681" "15968723404.2553" "18504760638.2979" "21730000000"      "25710904255.3191" "22938218085.1064" "25713271276.5957"
              "BHS" "8317830000"       "8881160000"       "8870090000"       "9055290000"       "9836200000"       "10167250000"      "10618340000"      "10526000000"      "9981960000"       "10095760000"     
              "BIH" "5800774707.17423" "6728771318.57555" "8498560877.09175" "10157553968.254"  "11222953328.6704" "12864610865.2428" "15778767389.7831" "19112739364.8892" "17613836494.0692" "17176781065.8902"
              "BLR" "12354820143.8849" "14594249022.8922" "17827791321.3067" "23144351851.8519" "30207567316.6202" "36954312354.3124" "45277399813.6067" "60763483146.0674" "50874078052.2735" "57222490768.7143"
              "BLX" "21387533703.2327" "23649833332.1655" "29667268248.1305" "35064843792.8993" "37672280120.4794" "42910146296.0646" "51587401415.7872" "58844277701.5258" "54467289897.5582" "56213985987.4168"
              "BLX" "236746141604.37"  "258383599375.177" "318082528506.588" "369214712443.206" "385714762230.039" "408259840868.823" "470922156309.453" "517328087920.078" "483254171097.812" "481420882905.001"
              "BLZ" "871590369.32848"  "932292858.88771"  "990727709.549365" "1057653620.03465" "1111453823.62023" "1211148304.51368" "1283112549.99844" "1352588867.72777" "1320605471.63149" "1387925188.49004"
              "BMU" "3680483000"       "3937228000"       "4186525000"       "4484703000"       "4868136000"       "6144000000"       "6767000000"       "6980000000"       "6656000000"       "6634526000"      
              end
              OK.Now look at the above dataset, it appears "BLX" twice, and I want to sum the values of the two rows. For example, plus "21387533703.2327" and "236746141604.37".

              Comment


              • #8
                Ah okay, that helps. Consider this code:

                Code:
                drop if missing(countrycode)
                
                forvalues x = 2001/2010 {
                    destring yr`x', replace force
                }
                
                collapse (sum) yr*, by(countrycode)
                Last edited by Hemanshu Kumar; 11 Aug 2022, 01:53.

                Comment


                • #9
                  Originally posted by Hemanshu Kumar View Post
                  Ah okay, that helps. Consider this code:

                  Code:
                  drop if missing(countrycode)
                  
                  forvalues x = 2001/2010 {
                  destring yr`x', replace force
                  }
                  
                  collapse (sum) yr*, by(countrycode)
                  It works very well. I admire that you can use orders so skillfully. So could you be so kind as to share some links to learn Stata? Thank you!

                  Comment


                  • #10
                    I am glad it was useful. One useful resource for learning Stata is the UCLA site, though of course there are many others. The -collapse- command I used is discussed here. This page covers many programming tips, including the use of macros.

                    Comment


                    • #11
                      Originally posted by Hemanshu Kumar View Post
                      I am glad it was useful. One useful resource for learning Stata is the UCLA site, though of course there are many others. The -collapse- command I used is discussed here. This page covers many programming tips, including the use of macros.
                      I get it. Thank you!

                      Comment


                      • #12
                        #6 Yes, the code in #4 depends on each countrycode occurring just once.

                        #2 #8 There is no need to destring in a loop. destring accepts multiple variables in a single call, even all of them when that makes sense.


                        Code:
                        destring yr*, replace ignore(".")
                        should work.

                        Comment


                        • #13
                          Originally posted by Nick Cox View Post
                          #6 Yes, the code in #4 depends on each countrycode occurring just once.

                          #2 #8 There is no need to destring in a loop. destring accepts multiple variables in a single call, even all of them when that makes sense.


                          Code:
                          destring yr*, replace ignore(".")
                          should work.
                          OK. I will try your method. Thanks a lot.

                          Comment

                          Working...
                          X