Announcement

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

  • Creating State variables from city codes

    Dear All,

    I am trying to create a new "state" variable from certain city codes, one can observe that each city code has its own state number at the beginning. For example, 5 is state A and 99 is state Z. I was wondering if it is possible to extract the first (or first two) numbers from a city code and from that, create a new variable which assigns it to a state. I realize this is possible but due to my unfamiliarity with loops I would like to ask for anybody's help.

    Thank you very much.

    Juan

  • #2
    It is indeed possible, as you realize, and it doesn't require loops, as you seem to believe, but in any event the exact technique depends very much on the specific format of your data.

    Even the best descriptions of data are no substitute for an actual example of the data. There are many ways your data might be organized that are consistent with your description, and each would require a somewhat different approach. In order to get a helpful response, you need to show some example data.

    Be sure to use the dataex command to do this. If you are running version 15.1 or a fully updated version 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 and 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.

    When asking for help with code, always show example data. When showing example data, always use dataex.

    Comment


    • #3
      Dear William, thanks for the suggestion, I've included an example of the city codes below:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input double codmpio
      5001
      5002
      5004
      5021
      52233
      52234
      52235
      52236
      63470
      63471
      63472
      63473
      68264
      68263
      68265
      68266
      70823
      70824
      70825
      70826
      end
      Where the first code, 5, corresponds to state 5. 52 to state 52, 63 to state 63 and so on. The state dataset is unavailable but I wish to create one from the data. I hope this helps. The point is creating a command so as to assign these city codes to a different state.


      Comment


      • #4
        Code:
        gen state_code = floor(codmpio/1000)

        Comment

        Working...
        X