Announcement

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

  • Creating basic panel structure in Stata for large dataset

    Dear all,

    I am using Stata version 14.2 on Windows 7 and I am trying to create a basic panel structure in Stata for 7332 individuals, which are followed for 17 time periods (1999 to 2015).
    To create the time variable, I used the following code:

    Code:
    set obs 124644
    egen t = fill(1999 2000 to 2015  1999 2000 to 2015)
    format %ty t
    Now I need to create the corresponding individual unique ID. The panel structure should look like:
    Year ID
    1999 1
    2000 1
    ... ...
    2015 1
    1999 2
    2000 2
    ... ...
    2015 2
    1999 3
    2000 3
    etc etc
    Does someone know a smart command how to create the ID variable starting with 1 and ending with 7332 for each 17 time periods?
    How would the work with a loop command?

    The aim is to use this basic structure to match several datasets (based on the variables year and unique ID).

    If this questions has been addressed before, I would be happy if you could provide me with the right directions.

    Thank you and best
    Manuel

  • #2
    Code:
    set obs 7332
    gen id = _n
    expand 17
    bys id : gen year = 1998 + _n
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Try:
      Code:
      bysort Year : gen ID=_n

      Comment


      • #4
        Maarten replied first.
        His code will create both id and year variable as you want.
        Mine supposes that the Year variable is already created, as I though it was the case reading the initial post.

        Comment


        • #5
          Very quick reply, thank you - both works!

          With Charlie's command I added
          Code:
          sort ID year
          Best
          Manuel

          Comment


          • #6
            In addition, this works:

            Code:
            set obs 124644
            egen year = seq(), from(1999) to(2015) 
            egen id = seq(), block(17)

            Comment

            Working...
            X