Announcement

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

  • Joining Alpha-3 Country Codes

    Dear All,

    I am looking for a way to automatically generate Alpha-3 Country codes from Country Names. Normally, when I merge two different time-series datasets, I merge on country names. But this is a huge problem because some cells are left blank when the country names are different. Then I have to manually copy-paste all the missing cells.I can easily generate an automatic group id for each Country-year dyad, but really don't know how to generate the Alpha-3 codes using a global standard. I think STATA might have a package for this.

    My data:

    Country Year Chnaid

    Afghanistan 2001 Value1
    Afghanistan 2002 Value2
    ......

    What I want to have:

    Country ccode Year Chnaid

    Afghanistan AFG 2001 Value1
    Afghanistan AFG 2002 Value 2
    Afghanistan AFG 2003 Value 3

    Best Regards
    Nihat.
    Last edited by Nihat Mugurtay; 19 Apr 2022, 07:50.

  • #2
    There's no package for this.

    The code however (without an example from dataex as you're asked to use to show your data) will involve the split command, presuming your data look how I presume they do.

    Please give your example data using the dataex command. For us to provide meaningful feedback, you must provide your example data using the dataex command, the real data from an easily importable source (i.e., Github), or the equivalent of a toy example.
    Otherwise, anything we say is simply a waste of time. Note, that I'm not trying to be mean in saying this, I'm saying this because if we can't see your dataset as you do with a minimal worked example, anything we suggest is just guesswork. The reason that I'm emphasizing this is because questions like this one likely have a relatively simple fix, but even simple fixes can be wildly overcomplicated without a minimal worked example of a dataset and code that you've tried to accomplish your task.

    So please, provide us with your example data that encapsulates the problem and I'm more than okay with helping you solve this.

    Comment


    • #3
      Code:
      ssc install kountry
      clear
      input str20 cname
      "Afghanistan"
      "United States"
      "South Africa"
      end
      kountry cname, from(other) stuck
      list
      rename _ISO3N_ iso
      kountry iso, from(iso3n) to(iso3c)
      list

      Comment


      • #4
        Dear Rafal,

        I really thank you. It worked and saved my time a lot.

        Best Regards,

        Comment


        • #5
          Dear Rafal Raciborski Rafal Raciborski (StataCorp)

          Thank you so much for your suggestion, it helped a lot. But, I have another problem.

          Now, I started to join regions for all the countries in my dataset, however, I wonder if I can have a solution for this in STATA 17.
          For instance, can I automatically generate regions from ISO Numeric or string Alpha-3 codes?

          Best wishes

          For instance (I know this example non-sense, but just a prediction)

          Code:
             
           ssc install kregion  kregion rname, from(other) stuckby(iso)
          Last edited by Nihat Mugurtay; 09 May 2022, 13:52.

          Comment


          • #6
            Hello Nihat,

            kountry has a geo() option so you can specify something like

            Code:
            kountry iso, from(iso3n) to(iso3c) geo(undet)

            Comment


            • #7
              Dear Rafal Raciborski Rafal Raciborski (StataCorp)

              This is amazing. It worked.

              Best,

              Comment


              • #8
                Dear Rafal Raciborski, Rafal Raciborski (StataCorp) ,

                Your codes worked well for my purposes, but I want to ask a very specific question. Your code smoothly functioned well as below.

                Code:
                kountry cname, from(other) stuck
                list
                rename _ISO3N_ iso
                kountry iso, from(iso3n) to(iso3c)
                list
                However, I did not understand why would we use this part below?

                Code:
                clear
                input str20 cname
                "Afghanistan"
                "United States"
                "South Africa"
                end

                Comment


                • #9
                  Originally posted by Nihat Mugurtay View Post
                  Dear Rafal Raciborski, Rafal Raciborski (StataCorp) ,

                  Your codes worked well for my purposes, but I want to ask a very specific question. Your code smoothly functioned well as below.

                  Code:
                  kountry cname, from(other) stuck
                  list
                  rename _ISO3N_ iso
                  kountry iso, from(iso3n) to(iso3c)
                  list
                  However, I did not understand why would we use this part below?

                  Code:
                  clear
                  input str20 cname
                  "Afghanistan"
                  "United States"
                  "South Africa"
                  end
                  I think that was only to provide a small use case for how it works. I tested it and its neat.

                  Comment


                  • #10
                    Dear Girish Venkataraman,

                    Yes, actually this code saved my time a lot.

                    Comment


                    • #11
                      Dear Rafal Raciborski Rafal Raciborski (StataCorp)

                      The solution you provided was extremely helpful. However, there are some countries -some of them are de-facto states- that I need to add manually. "North Macedonia", "Tokelau", "Kosovo", "West Bank and Gaza (as PSE)", "Cabo Verde", "Türkiye" (because it is not Turkey anymore in many databases) are examples of these countries. Is it possible to code them in a way that will not require manual intervention? Is there an updated version available?

                      Thank you in advance,

                      Best

                      Comment


                      • #12
                        Hello Nihat,

                        In case where some country names failed to convert to standardized names, kountryadd may be of use; see help kountryadd for more details.

                        Palestine and Tokelau are not part of kountry and you will need to process these cases separately.

                        Comment


                        • #13
                          Dear Rafal,

                          Thank you so much the information.

                          Best,

                          Comment

                          Working...
                          X