Announcement

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

  • Merge yearly to period panel data

    I have two datasets. One is a panel dataset with year as time and id as unit dimension.

    Code:
    input year id x
    1 1 2
    2 1 3
    3 1 4
    4 1 5
    1 2 3
    2 2 4
    3 2 2
    4 2 5
    end
    The second one contains the same id but a time period

    Code:
    input str10 period int id
    "1-2" 1
    "2-4" 1
    "1-3" 1
    "2" 1
    "1" 2
    "2-4" 2
    "1-3" 2
    "4" 2
    end
    I want to merge these datasets so that I have the average value of x over that period in the second dataset. How can I do that?

  • #2
    Code:
    use second, clear
    gen year1 = real(substr(period,1,1))
    gen year2 = real(substr(period,-1,1))
    gen year3 = year2 - 1 if year2 - 1 > 1
    bys id: gen n = _n
    reshape long year, i(id n)
    merge m:1 year id using first
    collapse x, by(id period)
    Last edited by Øyvind Snilsberg; 06 Apr 2022, 06:36.

    Comment


    • #3
      Thanks, but in reality I have much more than three year intervals. This was just a coincidence in the toy example.

      Comment


      • #4
        I see, how about,
        Code:
        use second,clear
        gen p1 = real(substr(period,1,1))
        gen p2 = real(substr(period,-1,1))
        su p2
        expand r(max)
        bysort id period: gen year = _n
        keep if inrange(year,p1,p2)
        merge m:1 id year using first
        collapse x, by(id period)

        Comment


        • #5
          I think this fails because the periods have different lengths between id's. Sorry

          Comment


          • #6
            I have adapted the solution poster under #6 here
            https://www.statalist.org/forums/for...ls-in-the-rows

            To do part of the period reshape and then the previous solution posted here works.

            Comment

            Working...
            X