Announcement

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

  • Create specific ID

    Hi all !

    I have some data in the following format: State, District, Year, I want to create an ID such that it groups those three together. For example: Illinois (01), Chicago(03), Year(2018): 01032018

    Any help on how to go about this is much appreciated.

  • #2
    If you want it to include the constituents, as in the example you show, and if the State and District codes are always 2 digits, then you can do:

    Code:
    gen long id = 1000000*State + 10000*District + Year
    format id %08.0f
    Another way to get this same result is:
    Code:
    egen long id1 = concat(State District), format(%02.0f)
    egen long id = concat(id1 Year), format(%04.0f)
    Note: None of this code is tested as you did not provide example data.

    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 14.2, it 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.

    This code assumes that State, District, and Year are numeric variables. If they are strings, you must convert them first.

    That said, if all you need is a distinct numeric id for each combination of state, district, and year, and the numeric value of that id does not have to reflect the numbers identifying those components, you can just do
    Code:
    egen long id = group(State District Year)

    Comment


    • #3
      Thank you ! I will take note of the posting rules for the future.

      Comment


      • #4
        Update: I tried the code as stated, and it worked perfectly. Thank you once again !

        Comment

        Working...
        X