Announcement

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

  • Help with loop

    Hi,

    Getting used to the program. I'm having some trouble with a loop. In the first column of the data (which I import from an excel file), I have Country names (Country is the variable). The idea is to replace the entire country names (Brasil, Armenia, Colombia, etc) in each row of the first column for the corresponding 3 digit letter. So, the idea is to loop the following:

    replace Country = "ARM" in 1
    replace Country = "AUT" in 2
    replace Country = "AZE" in 3
    replace Country = "BLR" in 4
    replace Country = "BEL" in 5
    replace Country = "BOL" in 6
    replace Country = "BRA" in 7
    replace Country = "BGR" in 8
    replace Country = "COL" in 9


    Any suggestions? Thank you very much!!


  • #2
    You can check help -dateex- for how you can show us what your data looks like.

    Otherwise where is the correspondence rule, and in what form is the correspondence rule mapping full country names to 3 letter country names?

    Comment


    • #3
      Joro,

      Thank you very much for your answer. My data looks like this:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str18 Country double HLriskwithin
      "Armenia"             -.00014731838664908055
      "Austria"            -.000021316753985888387
      "Azerbaijan"             -.00135834729200614
      "Belarus"             -.00013787108293562383
      "Belgium"            -.000012721579385981468
      "Bolivia"                -.00448368039132419
      "Brazil"              -.00006198572816166327
      "Bulgaria"             -.0005510117705024879 
      end
      And I'm trying to loop:

      Code:
      replace Country = "ARM" in 1
      replace Country = "AUT" in 2
      replace Country = "AZE" in 3
      replace Country = "BLR" in 4
      replace Country = "BEL" in 5
      replace Country = "BOL" in 6
      replace Country = "BRA" in 7
      replace Country = "BGR" in 8
      I'm using world bank 3-letter country name abbreviation (https://wits.worldbank.org/wits/wits...ntry_codes.htm).

      Comment


      • #4
        I would do this by saving the World Bank correspondence file as a Stata .dta file, and then merge by Country. See -help merge-.

        Comment


        • #5
          I would start from the following code, selecting the countries you need and modifying the spelling as necessary to match the way the country is represented in your data. There is no need for a loop, and nothing to be gained by using one.
          Code:
          generate fullCountry = Country // in case you may need the full name later in your work
          replace Country="AFG" if Country=="Afghanistan"
          replace Country="ALB" if Country=="Albania"
          replace Country="DZA" if Country=="Algeria"
          replace Country="ASM" if Country=="American Samoa"
          replace Country="AND" if Country=="Andorra"
          replace Country="AGO" if Country=="Angola"
          replace Country="AIA" if Country=="Anguila"
          replace Country="ATG" if Country=="Antigua and Barbuda"
          replace Country="ARG" if Country=="Argentina"
          replace Country="ARM" if Country=="Armenia"
          replace Country="ABW" if Country=="Aruba"
          replace Country="AUS" if Country=="Australia"
          replace Country="AUT" if Country=="Austria"
          replace Country="AZE" if Country=="Azerbaijan"
          replace Country="BHS" if Country=="Bahamas, The"
          replace Country="BHR" if Country=="Bahrain"
          replace Country="BGD" if Country=="Bangladesh"
          replace Country="BRB" if Country=="Barbados"
          replace Country="BLR" if Country=="Belarus"
          replace Country="BEL" if Country=="Belgium"
          replace Country="BLX" if Country=="Belgium-Luxembourg"
          replace Country="BLZ" if Country=="Belize"
          replace Country="BEN" if Country=="Benin"
          replace Country="BMU" if Country=="Bermuda"
          replace Country="BTN" if Country=="Bhutan"
          replace Country="BOL" if Country=="Bolivia"
          replace Country="BIH" if Country=="Bosnia and Herzegovina"
          replace Country="BWA" if Country=="Botswana"
          replace Country="BAT" if Country=="Br. Antr. Terr"
          replace Country="BRA" if Country=="Brazil"
          replace Country="IOT" if Country=="British Indian Ocean Ter."
          replace Country="VGB" if Country=="British Virgin Islands"
          replace Country="BRN" if Country=="Brunei"
          replace Country="BGR" if Country=="Bulgaria"
          replace Country="BFA" if Country=="Burkina Faso"
          replace Country="BDI" if Country=="Burundi"
          replace Country="KHM" if Country=="Cambodia"
          replace Country="CMR" if Country=="Cameroon"
          replace Country="CAN" if Country=="Canada"
          replace Country="CPV" if Country=="Cape Verde"
          replace Country="CYM" if Country=="Cayman Islands"
          replace Country="CAF" if Country=="Central African Republic"
          replace Country="TCD" if Country=="Chad"
          replace Country="CHL" if Country=="Chile"
          replace Country="CHN" if Country=="China"
          replace Country="CXR" if Country=="Christmas Island"
          replace Country="CCK" if Country=="Cocos (Keeling) Islands"
          replace Country="COL" if Country=="Colombia"
          replace Country="COM" if Country=="Comoros"
          replace Country="ZAR" if Country=="Congo, Dem. Rep."
          replace Country="COG" if Country=="Congo, Rep."
          replace Country="COK" if Country=="Cook Islands"
          replace Country="CRI" if Country=="Costa Rica"
          replace Country="CIV" if Country=="Cote d'Ivoire"
          replace Country="HRV" if Country=="Croatia"
          replace Country="CUB" if Country=="Cuba"
          replace Country="CYP" if Country=="Cyprus"
          replace Country="CZE" if Country=="Czech Republic"
          replace Country="CSK" if Country=="Czechoslovakia"
          replace Country="DNK" if Country=="Denmark"
          replace Country="DJI" if Country=="Djibouti"
          replace Country="DMA" if Country=="Dominica"
          replace Country="DOM" if Country=="Dominican Republic"
          replace Country="TMP" if Country=="East Timor"
          replace Country="ECU" if Country=="Ecuador"
          replace Country="EGY" if Country=="Egypt, Arab Rep."
          replace Country="SLV" if Country=="El Salvador"
          replace Country="GNQ" if Country=="Equatorial Guinea"
          replace Country="ERI" if Country=="Eritrea"
          replace Country="EST" if Country=="Estonia"
          replace Country="ETH" if Country=="Ethiopia (excludes Eritrea)"
          replace Country="ETF" if Country=="Ethiopia (includes Eritrea)"
          replace Country="EUN" if Country=="European Union"
          replace Country="FRO" if Country=="Faeroe Islands"
          replace Country="FLK" if Country=="Falkland Island"
          replace Country="FJI" if Country=="Fiji"
          replace Country="FIN" if Country=="Finland"
          replace Country="PCZ" if Country=="Fm Panama Cz"
          replace Country="ZW1" if Country=="Fm Rhod Nyas"
          replace Country="TAN" if Country=="Fm Tanganyik"
          replace Country="VDR" if Country=="Fm Vietnam Dr"
          replace Country="SVR" if Country=="Fm Vietnam Rp"
          replace Country="ZPM" if Country=="Fm Zanz-Pemb"
          replace Country="ATF" if Country=="Fr. So. Ant. Tr"
          replace Country="FRA" if Country=="France"
          replace Country="FRE" if Country=="Free Zones"
          replace Country="GUF" if Country=="French Guiana"
          replace Country="PYF" if Country=="French Polynesia"
          replace Country="GAB" if Country=="Gabon"
          replace Country="GMB" if Country=="Gambia, The"
          replace Country="GAZ" if Country=="Gaza Strip"
          replace Country="GEO" if Country=="Georgia"
          replace Country="DDR" if Country=="German Democratic Republic"
          replace Country="DEU" if Country=="Germany"
          replace Country="GHA" if Country=="Ghana"
          replace Country="GIB" if Country=="Gibraltar"
          replace Country="GRC" if Country=="Greece"
          replace Country="GRL" if Country=="Greenland"
          replace Country="GRD" if Country=="Grenada"
          replace Country="GLP" if Country=="Guadeloupe"
          replace Country="GUM" if Country=="Guam"
          replace Country="GTM" if Country=="Guatemala"
          replace Country="GIN" if Country=="Guinea"
          replace Country="GNB" if Country=="Guinea-Bissau"
          replace Country="GUY" if Country=="Guyana"
          replace Country="HTI" if Country=="Haiti"
          replace Country="VAT" if Country=="Holy See"
          replace Country="HND" if Country=="Honduras"
          replace Country="HKG" if Country=="Hong Kong, China"
          replace Country="HUN" if Country=="Hungary"
          replace Country="ISL" if Country=="Iceland"
          replace Country="IND" if Country=="India"
          replace Country="IDN" if Country=="Indonesia"
          replace Country="IRN" if Country=="Iran, Islamic Rep."
          replace Country="IRQ" if Country=="Iraq"
          replace Country="IRL" if Country=="Ireland"
          replace Country="ISR" if Country=="Israel"
          replace Country="ITA" if Country=="Italy"
          replace Country="JAM" if Country=="Jamaica"
          replace Country="JPN" if Country=="Japan"
          replace Country="JTN" if Country=="Jhonston Island"
          replace Country="JOR" if Country=="Jordan"
          replace Country="KAZ" if Country=="Kazakhstan"
          replace Country="KEN" if Country=="Kenya"
          replace Country="KIR" if Country=="Kiribati"
          replace Country="PRK" if Country=="Korea, Dem. Rep."
          replace Country="KOR" if Country=="Korea, Rep."
          replace Country="KWT" if Country=="Kuwait"
          replace Country="KGZ" if Country=="Kyrgyz Republic"
          replace Country="LAO" if Country=="Lao PDR"
          replace Country="LVA" if Country=="Latvia"
          replace Country="LBN" if Country=="Lebanon"
          replace Country="LSO" if Country=="Lesotho"
          replace Country="LBR" if Country=="Liberia"
          replace Country="LBY" if Country=="Libya"
          replace Country="LIE" if Country=="Liechtenstein"
          replace Country="LTU" if Country=="Lithuania"
          replace Country="LUX" if Country=="Luxembourg"
          replace Country="MAC" if Country=="Macao"
          replace Country="MKD" if Country=="Macedonia, FYR"
          replace Country="MDG" if Country=="Madagascar"
          replace Country="MWI" if Country=="Malawi"
          replace Country="MYS" if Country=="Malaysia"
          replace Country="MDV" if Country=="Maldives"
          replace Country="MLI" if Country=="Mali"
          replace Country="MLT" if Country=="Malta"
          replace Country="MHL" if Country=="Marshall Islands"
          replace Country="MTQ" if Country=="Martinique"
          replace Country="MRT" if Country=="Mauritania"
          replace Country="MUS" if Country=="Mauritius"
          replace Country="MEX" if Country=="Mexico"
          replace Country="FSM" if Country=="Micronesia, Fed. Sts."
          replace Country="MID" if Country=="Midway Islands"
          replace Country="MDA" if Country=="Moldova"
          replace Country="MCO" if Country=="Monaco"
          replace Country="MNG" if Country=="Mongolia"
          replace Country="MSR" if Country=="Montserrat"
          replace Country="MAR" if Country=="Morocco"
          replace Country="MOZ" if Country=="Mozambique"
          replace Country="MMR" if Country=="Myanmar"
          replace Country="NAM" if Country=="Namibia"
          replace Country="NRU" if Country=="Nauru"
          replace Country="NPL" if Country=="Nepal"
          replace Country="NLD" if Country=="Netherlands"
          replace Country="ANT" if Country=="Netherlands Antilles"
          replace Country="NZE" if Country=="Neutral Zone"
          replace Country="NCL" if Country=="New Caledonia"
          replace Country="NZL" if Country=="New Zealand"
          replace Country="NIC" if Country=="Nicaragua"
          replace Country="NER" if Country=="Niger"
          replace Country="NGA" if Country=="Nigeria"
          replace Country="NIU" if Country=="Niue"
          replace Country="NFK" if Country=="Norfolk Island"
          replace Country="MNP" if Country=="Northern Mariana Islands"
          replace Country="NOR" if Country=="Norway"
          replace Country="OMN" if Country=="Oman"
          replace Country="PCE" if Country=="Pacific Islands"
          replace Country="PAK" if Country=="Pakistan"
          replace Country="PLW" if Country=="Palau"
          replace Country="PAN" if Country=="Panama"
          replace Country="PNG" if Country=="Papua New Guinea"
          replace Country="PRY" if Country=="Paraguay"
          replace Country="PMY" if Country=="Pen Malaysia"
          replace Country="PER" if Country=="Peru"
          replace Country="PHL" if Country=="Philippines"
          replace Country="PCN" if Country=="Pitcairn"
          replace Country="POL" if Country=="Poland"
          replace Country="PRT" if Country=="Portugal"
          replace Country="PRI" if Country=="Puerto Rico"
          replace Country="QAT" if Country=="Qatar"
          replace Country="REU" if Country=="Reunion"
          replace Country="ROM" if Country=="Romania"
          replace Country="RUS" if Country=="Russian Federation"
          replace Country="RWA" if Country=="Rwanda"
          replace Country="RYU" if Country=="Ryukyu Is"
          replace Country="SBH" if Country=="Sabah"
          replace Country="SHN" if Country=="Saint Helena"
          replace Country="KN1" if Country=="Saint Kitts-Nevis-Anguilla-Aru"
          replace Country="SPM" if Country=="Saint Pierre and Miquelon"
          replace Country="WSM" if Country=="Samoa"
          replace Country="SMR" if Country=="San Marino"
          replace Country="STP" if Country=="Sao Tome and Principe"
          replace Country="SWK" if Country=="Sarawak"
          replace Country="SAU" if Country=="Saudi Arabia"
          replace Country="SEN" if Country=="Senegal"
          replace Country="SYC" if Country=="Seychelles"
          replace Country="SLE" if Country=="Sierra Leone"
          replace Country="SIK" if Country=="SIKKIM"
          replace Country="SGP" if Country=="Singapore"
          replace Country="SVK" if Country=="Slovak Republic"
          replace Country="SVN" if Country=="Slovenia"
          replace Country="SLB" if Country=="Solomon Islands"
          replace Country="SOM" if Country=="Somalia"
          replace Country="ZAF" if Country=="South Africa"
          replace Country="SVU" if Country=="Soviet Union"
          replace Country="ESP" if Country=="Spain"
          replace Country="SPE" if Country=="Special Categories"
          replace Country="LKA" if Country=="Sri Lanka"
          replace Country="KNA" if Country=="St. Kitts and Nevis"
          replace Country="LCA" if Country=="St. Lucia"
          replace Country="VCT" if Country=="St. Vincent and the Grenadines"
          replace Country="SDN" if Country=="Sudan"
          replace Country="SUR" if Country=="Suriname"
          replace Country="SJM" if Country=="Svalbard and Jan Mayen Is"
          replace Country="SWZ" if Country=="Swaziland"
          replace Country="SWE" if Country=="Sweden"
          replace Country="CHE" if Country=="Switzerland"
          replace Country="SYR" if Country=="Syrian Arab Republic"
          replace Country="TWN" if Country=="Taiwan"
          replace Country="TJK" if Country=="Tajikistan"
          replace Country="TZA" if Country=="Tanzania"
          replace Country="THA" if Country=="Thailand"
          replace Country="TGO" if Country=="Togo"
          replace Country="TKL" if Country=="Tokelau"
          replace Country="TON" if Country=="Tonga"
          replace Country="TTO" if Country=="Trinidad and Tobago"
          replace Country="TUN" if Country=="Tunisia"
          replace Country="TUR" if Country=="Turkey"
          replace Country="TKM" if Country=="Turkmenistan"
          replace Country="TCA" if Country=="Turks and Caicos Isl."
          replace Country="TUV" if Country=="Tuvalu"
          replace Country="UGA" if Country=="Uganda"
          replace Country="UKR" if Country=="Ukraine"
          replace Country="ARE" if Country=="United Arab Emirates"
          replace Country="GBR" if Country=="United Kingdom"
          replace Country="USA" if Country=="United States"
          replace Country="UNS" if Country=="Unspecified"
          replace Country="URY" if Country=="Uruguay"
          replace Country="USP" if Country=="Us Msc.Pac.I"
          replace Country="UZB" if Country=="Uzbekistan"
          replace Country="VUT" if Country=="Vanuatu"
          replace Country="VEN" if Country=="Venezuela"
          replace Country="VNM" if Country=="Vietnam"
          replace Country="VIR" if Country=="Virgin Islands (U.S.)"
          replace Country="WAK" if Country=="Wake Island"
          replace Country="WLF" if Country=="Wallis and Futura Isl."
          replace Country="ESH" if Country=="Western Sahara"
          replace Country="WLD" if Country=="World"
          replace Country="YDR" if Country=="Yemen Democratic"
          replace Country="YEM" if Country=="Yemen, Rep."
          replace Country="SER" if Country=="Yugoslavia"
          replace Country="YUG" if Country=="Yugoslavia, FR (Serbia/Montene"
          replace Country="ZMB" if Country=="Zambia"
          replace Country="ZWE" if Country=="Zimbabwe"

          Comment


          • #6
            After you have done what William suggests, you will likely need/want a numeric value to represent each country. To get that, with the country abbreviation as a label:
            Code:
            encode Country, gen(country_code)
            Stata/MP 14.1 (64-bit x86-64)
            Revision 19 May 2016
            Win 8.1

            Comment


            • #7
              I think the simplest solution is to go to the worldbank.org link shown in #3, copy that table, and paste it into the Stata Data Editor to create a new data set that is a crosswalk between country names and the 3-letter codes. Then -merge- that with your data. You may find some mismatches due to variant spellings or variant names in your data, but you can then fix those individually.

              Comment


              • #8
                For what it's worth, my code in post #5 was generated from the table at the link in post #3. Copy and paste of that table into the Stata Data Editor, or into Excel, or a text editor, does not work so well on my Mac copying from Safari: the three columns of a single row become one column in three successive rows.

                Comment


                • #9
                  Thank you very much guys!!!

                  Comment

                  Working...
                  X