Announcement

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

  • create unique county ID for each state

    I have a panel data. I want to assign a unique ID to each county within each state. For example, for all county within Maryland state, I want to generate a unique county ID (Baltimore: 1, Prince George: 2, and so on). Similarly, each county within New York State will have a separate ID (Queens: 1, Rockland: 2, and so on). I want to do this for each states in the US. Can I use the "foreach" command? Or is there a better way?
    Thank you.

  • #2
    Giving us an idea of the structure of your data will help you get an answer faster.

    if your dataset is something like this:
    Code:
    input str2 state str20 county 
    MD “Baltimore”
    MD “Prince George”
    NY “Queens”
    NY “Rockland”
    end
    Then
    Code:
    egen statecounty = group(state county)
    will assign a unique number to each state-county pair

    Comment


    • #3
      Thank you Arthur.

      My data looks like below, where states are already coded (1: NY, 2: MD, 3: VA):

      input state str20 county
      1 "Queens"
      1 "Rockland"
      1 "Suffolk"
      2 "Baltimore"
      2 "Prince George"
      2 "Anne Arundle"
      3 "Richmond"
      3 "Fairfax"

      I want to create a new variable for county code (county_cd) which assigns values 1 through n (last county number within each state) for each state. So, for each state county codes will start from 1 through the last county number in that state. I hope I am able to explain my requirement more appropriately.

      Many thanks.

      Comment


      • #4
        Is there a defined order of counties within states that you need to maintain? In Kenya, for example, the coastal city of Mombasa lies in county #1 and the inland capital of Nairobi lies in county # 47.
        https://www.tuko.co.ke/277256-list-c...ers-kenya.html

        If this is the case with your list of counties, then you obviously need to merge these county codes with the names of counties in your dataset. If there is no defined order, then the procedure is straightforward.

        Code:
        egen statecounty = group(state county)
        bys state (statecounty): replace statecounty=_n
        Last edited by Andrew Musau; 24 Feb 2020, 08:56.

        Comment

        Working...
        X