Announcement

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

  • Generate an indicator for number of observations by dyad with multiple imputated panel data

    Dear Statalist,
    I would have a basic question and came to no clue after intensive search over the web.

    I am using a panel dataset with repeated observations for individuals (and dyads) over several waves. However, I have used multiple imputation (10 imputations) and I cannot create a variable that counts for how many waves the dyads are in the dataset. Obviously, since I have repeated measurements for each of the 10 imputed dataset, I cannot apply something like:

    Code:
    bys dyad: gen nyear=[_N]
    Would you have any practical solutions to this problem?



    Below you can find an example of my data structure ("_mi_m" is the imputation dataset number)

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float pid byte(dyad wave _mi_m)
    1 1 1  1
    1 1 1  2
    1 1 1  3
    1 1 1  4
    1 1 1  5
    1 1 1  6
    1 1 1  7
    1 1 1  8
    1 1 1  9
    1 1 1 10
    1 1 2  1
    1 1 2  2
    1 1 2  3
    1 1 2  4
    1 1 2  5
    1 1 2  6
    1 1 2  7
    1 1 2  8
    1 1 2  9
    1 1 2 10
    1 1 4  0
    1 1 5  0
    1 2 1  1
    1 2 1  2
    1 2 1  3
    1 2 1  4
    1 2 1  5
    1 2 1  6
    1 2 1  7
    1 2 1  8
    1 2 1  9
    1 2 1 10
    1 2 2  1
    1 2 2  2
    1 2 2  3
    1 2 2  4
    1 2 2  5
    1 2 2  6
    1 2 2  7
    1 2 2  8
    1 2 2  9
    1 2 2 10
    1 2 4  0
    1 2 5  0
    2 4 1  1
    2 4 1  2
    2 4 1  3
    2 4 1  4
    2 4 1  5
    2 4 1  6
    2 4 1  7
    2 4 1  8
    2 4 1  9
    2 4 1 10
    2 4 2  1
    2 4 2  2
    2 4 2  3
    2 4 2  4
    2 4 2  5
    2 4 2  6
    2 4 2  7
    2 4 2  8
    2 4 2  9
    2 4 2 10
    2 4 3  1
    2 4 3  2
    2 4 3  3
    2 4 3  4
    2 4 3  5
    2 4 3  6
    2 4 3  7
    2 4 3  8
    2 4 3  9
    2 4 3 10
    2 4 4  0
    3 3 2  0
    3 3 4  0
    3 3 5  0
    end
    Last edited by Matthew Campbell; 20 Apr 2022, 09:34.

  • #2
    Code:
    bysort dyad (wave): egen wanted = total(wave!=wave[_n-1])

    Comment


    • #3
      Dear Ali Atia,
      thank you so much for this! Your solution solved my problem... This was exactly what I was looking for.

      Comment


      • #4
        An alternative formulation is as the count of distinct values.

        Code:
        egen tag = tag(dyad wave) 
        egen wanted = total(tag), by(dyad)
        See https://www.stata-journal.com/articl...article=dm0042

        Comment


        • #5
          Dear Nick Cox,
          many thanks for this suggestion. Yes, I also think that this formulation is more intuitive. I will check the paper you suggests ("Speaking Stata: Distinct observations") and also the distinct command.

          Comment

          Working...
          X