Announcement

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

  • Generating years in panel data and mapping them to 'person id'

    Click image for larger version

Name:	Screenshot 2024-02-05 at 18.03.59.png
Views:	1
Size:	271.3 KB
ID:	1742159

    Hi everyone,
    I am working with survey data. It is panel data over the course of 11 years (1991-2002). I want to use "xtset pid year" to declare to STATA that my data is panel data with "pid" (person id) and "year" (for each of the 11 waves of the survey). There is a problem though. My panel data does not have year-wise data. It has tons of variables, with each variable prefixed A for wave one, B for wave two, C for wave three and so on, until K for wave 11. Some examples are: 'ahid' for the household ID in wave 1, 'bhid' for household ID in wave 2, and so on. (You can have a sneak at a wide form of the data in the screenshot above!)
    I have been trying to hard to extract the prefix alphabet and map 'a' to 1991, 'b' to 1992 and so on but I'm just failing at this. I'm so stressed and I cannot figure out how to fix this! I would be forever indebted for any help that you could give me.

    Thank you so much!
    Last edited by sladmin; 15 Mar 2024, 16:19. Reason: anonymize original poster

  • #2
    This might work.

    Code:
    clear
    
    set obs 10
    g pid = _n
    g sex = runiform()>0.5
    
    foreach z in a b c d e f g h i j k {
        g `z'hid = runiform()
        g `z'pno = runiform()
    }
    
    local yr = 1991
    foreach z in a b c d e f g h i j k {
         rename `z'hid hid`yr'
         rename `z'pno pno`yr'
         local yr = `yr'+1
    }
    reshape long hid pno, i(pid) j(year)

    Comment


    • #3
      Thank you George, the -reshape long- command did it for me!

      Comment

      Working...
      X