Announcement

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

  • Replace command with multiple if (string) values

    Hi everyone,

    I have a lot of countries (written as country names (string), example: Albania, no country codes) in my data and I want to generate a variable called region that categorizes them into 5 regions (1=Europe, 2= Middle East, 3=Asia, 4=Africa, 5=Americas). However, I can only do it one "replace" command per country which is tedious. I'm sure there's a more efficient way that I'm not aware of. Any help?

    Many thanks,
    Yasmine

  • #2
    There is a user-written package, -kountry-, by Rafal Raciborski, available both from SSC and Stata Journal that I believe will do this for you.

    Comment


    • #3
      Code:
      ssc install kountry, replace
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str52 country
      "Australia"             
      "Austria"               
      "Azerbaijan"            
      "Burundi"               
      "Belgium"               
      "Benin"                 
      "Burkina Faso"          
      "Bangladesh"            
      "Bulgaria"              
      "Bahrain"               
      "Bahamas, The"          
      "Bosnia and Herzegovina"
      "Belarus"               
      "Belize"                
      "Bermuda"               
      "Bolivia"               
      "Brazil"                
      end
      
      kountry country, from(other) geo(cow)
      Depending on how obscure are your country names, you may need to assign some regions manually. Also note the existence of other regional classifications. See

      Code:
      help kountry
      Res.:

      Code:
      . l, sep(0)
      
           +---------------------------------------------------------------------------+
           |                country                NAMES_STD                       GEO |
           |---------------------------------------------------------------------------|
        1. |              Australia                Australia                      Asia |
        2. |                Austria                  Austria                    Europe |
        3. |             Azerbaijan               Azerbaijan                    Europe |
        4. |                Burundi                  Burundi                    Africa |
        5. |                Belgium                  Belgium                    Europe |
        6. |                  Benin                    Benin                    Africa |
        7. |           Burkina Faso             Burkina Faso                    Africa |
        8. |             Bangladesh               Bangladesh                      Asia |
        9. |               Bulgaria                 Bulgaria                    Europe |
       10. |                Bahrain                  Bahrain               Middle East |
       11. |           Bahamas, The                  Bahamas   North and South America |
       12. | Bosnia and Herzegovina   Bosnia and Herzegovina                    Europe |
       13. |                Belarus                  Belarus                    Europe |
       14. |                 Belize                   Belize   North and South America |
       15. |                Bermuda                  Bermuda                           |
       16. |                Bolivia                  Bolivia   North and South America |
       17. |                 Brazil                   Brazil   North and South America |
           +---------------------------------------------------------------------------+

      Comment

      Working...
      X