Announcement

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

  • Creating dummy for unique value

    Hey,

    For my dataset I have 20+ years of data with 19 unique countries per year

    I want to create 1 column where every country gets its own dummy number, without typing everything out like so:

    generate countrydummy = 0
    replace countrydummy =1 if Country == " Austria"
    replace countrydummy =2 if Country == "Belgium"
    etc..


    Is there a way to efficiently create a dummy for every country? (I do not want to use: tabulate Country, generate(countrydummy) because I will get 19 columns.. )

  • #2
    The statement

    Code:
    egen countrydummy = group(Country)
    will mechanically achieve what you have stated that you want to do. But the result is not a dummy variable, and it is not clear why would you want to generate such a variable.

    Comment


    • #3
      The encode command will easily create a categorical variable with values 1-19, and additionally will create a value label for the variable, so that country names - rather than their numeric code - can appear in listings, tabulations, etc.

      See
      Code:
      help encode
      for details.

      Once you've created your numeric categorical variable, you can include it in your models using Stata's factor variable notation: see
      Code:
      help factor variables
      for details.

      Comment


      • #4
        William is right of course that -encode- can be used too.

        Curiously enough not many users realise and use -egen, group()-, which with the label option offers the same functionality as -encode-. E.g.,

        Code:
         
         egen countrydummy = group(Country), label
        will do the same as -encode-.

        Comment


        • #5
          Thank you both! This is indeed what I was looking for, funny that the code is so easy yet hard to find when looking for it online

          Comment


          • #6
            hard to find when looking for it online
            If by "online" you mean "searching the web", let me describe a more productive approach.

            When I began using Stata in a serious way, I started - as others here did - by reading my way through the Getting Started with Stata manual relevant to my setup. Chapter 18 then gives suggested further reading, much of which is in the Stata User's Guide, and I worked my way through much of that reading as well. All of these manuals are included as PDFs in the Stata installation and are accessible from within Stata - for example, through Stata's Help menu. The objective in doing this was not so much to master Stata as to be sure I'd become familiar with a wide variety of important basic techniques, so that when the time came that I needed them, I might recall their existence, if not the full syntax, and know how to find out more about them in the help files and manual.

            Stata supplies exceptionally good documentation that amply repays the time spent studying it - there's just a lot of it. The path I followed surfaces the things you need to know to get started in a hurry and to work effectively.


            Comment

            Working...
            X