Announcement

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

  • Altering long postcodes into 2 letters and creating dummy variables

    Dear Statalist community,

    I am trying to convert 4+ digit postcodes into only 2 (letters) so that I have fewer categories (dummies), however, I cannot find a command to do this.

    After I manage to shorten the postcode, how do I create dummies for them (given they are categorical variables)?

    Any help would be really appreciated!

    Thank you a lot,
    M

  • #2
    As you show no example data, I doubt anyone can answer your question. Without knowing what these postcodes actually look like, it's just impossible to say. Actually, your description makes it sound impossible anyway: if the postcodes are 4+ digits, on what basis would letters be chosen to represent them?

    So please post back with example data. And use the -dataex- command to do that. If you are running version 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.

    Why do you want to create "dummies" for them? If it's to use them in a regression of some kind, there is no need for that unless you are using an ancient version of Stata. Use factor-variable notation instead. (See -help fvvarlist- for details).

    Comment


    • #3
      Potentially, none of this is needed. If you just want to use dummies in a regression, for example, Stata can do that for you on the fly without the need to create variables per se. See
      Code:
      help fvvarlist
      For instance, if your variable is postcode and you want to include postcode-level fixed effects in an OLS regression, you could just do something like

      Code:
      regress y x i.postcode
      If, on the other hand, you want to combine values of postcode into fewer categories, you would need to do something different. Is that what you want, or does the above suffice?

      Comment

      Working...
      X