Announcement

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

  • Fill in one column based on another

    Hello,

    I've been looking for a solution to tie the country code's name to their actual names, in a panel data. One of the things I've come up with is to create a countryid where each country is assigned a particular number. (Please see picture)
    Now I'm thinking is there's a simple command to assign for instance each number to a specific country name. e.g. when countryid is 1 country takes Angola for instance.

    Any other suggestions are welcome, cheers!

    Click image for larger version

Name:	Screenshot 2024-01-22 at 14.25.15.png
Views:	1
Size:	246.7 KB
ID:	1740725

  • #2
    Code:
    egen wanted = group(country)

    Comment


    • #3
      Clyde Schechter Thank you for the reply. I did put that it generated another column 'wanted' and all enteries are dots. Country column still blank. I don't know what's the command supposed to do

      Comment


      • #4
        Perhaps I have misunderstood what you already have. I assumed that you already had a variable, country, which is a string variable that contains the names of the countries in your data set, e.g. "Angola." If that is what you had, the code I showed would create a new numeric variable, wanted, which would assign each country a numeric code, starting from 1 and counting up in alphabetical order.

        But reflecting again on #1, I see that your country variable in the screenshot you show has nothing at all in it. It is just an empty string variable, and you already have a numeric variable called countryid--but there is nothing in what show that tells you which country each of the numbers in countryid stands for.

        So the question back to you is where did the variable countryid come from? Somewhere in the source of this data I would expect you can find a correspondence between those numbers and the names of the countries. Depending on how that is set up, there is probably an easy way to bring that information along into what you have. So please explain that.

        Also, before posting back, please do read the Forum FAQ, with particular emphasis on #12. There it is explained that screenshots of data are not really helpful. The helpful way to show example data is with the -dataex- command. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

        Comment


        • #5
          If you start off with a string variable with the country names (named "str_country" below), then you can create the numerical identifier with the country names as value labels using encode.

          Code:
          encode str_country, gen(country_id)
          lab list
          See

          Code:
          help encode

          Comment

          Working...
          X