Announcement

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

  • Rename Country in a Dataset

    Hi,

    I have a dataset for many countries. Nevertheless, I would like to change some names. For instance, from "Bolivia(Plurinational State of)" to simply "Bolivia" as shown in the picture. What is the code to do it quickly instead of manually going to Data editor and changing the name? (My objective is to merge this dataset with another one so I want to keep names very uniform so I do not lose data)




    Thanks,

    Hugo
    Attached Files

  • #2
    First, it'd be better to document all the data cleaning and changes in a text file called a "do file." Here are a couple resources;

    https://www.youtube.com/watch?v=6syxC5jJz-s
    https://www.stata.com/manuals/u16.pdf

    The advantage is that users would no long need to point and click and change in the data editor. Instead, all changes are done through commands and i) it's more traceable, and ii) the do file can be reused.

    Second, it's often safer to create a new country variable rather than overwriting the old one. That way you'd know which ones have been renamed. Then you can use the replace command to change the entries:

    Code:
    gen country_renamed = NAMES_STD
    replace country_renamed = "Bolivia" if country_renamed == "Bolivia (Plurinational State of)"
    replace country_renamed = "Another Country" if country_renamed == "Another Country Unwanted Name"
    replace country_renamed = "Another Country 2" if country_renamed == "Another Country Unwanted Name 2"
    So on, so forth. Once you're done typing. Save the do file for future record. And then execute it with the Do button. Check that PDF document on how to execute the do file on a Mac.
    Last edited by Ken Chui; 04 Apr 2021, 15:21.

    Comment


    • #3
      Code:
      replace NAMES_STD = "Bolivia" if NAMES_STD == "Bolivia (Plurinational State of)"
      Thank you for attempting to use -dataex-. Unfortunately, by posting a screenshot of the -dataex- output you defeated the purpose. The whole point of using -dataex- is that somebody who wants to help you can copy the -dataex- output you post, paste it into the Stata do-file editor, run it, and thereby have a faithful replica of your data set, including correct metadata. But you cannot copy/paste with a screenshot. Screenshots are great for human eyes, but useless for most computer purposes. In the future, please use -dataex- correctly: copy the -dataex- output from your Results window and paste it here into the Forum editor.

      Added: Crossed with #2.

      Comment


      • #4
        Thanks you both! I am sorry, I should have copied the code! Yes, I do have my dofile with my analysis! Thanks again

        Comment

        Working...
        X