Announcement

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

  • Combining multiple observations with same unique ID into one observation

    Hello,

    I want to combine multiple observations with same unique ID into a single observation and replace the missing values of the variables if the value exists in any of the duplicates.
    I have tried to search in the forum, however most solutions offered work if there is 1 copy of duplicate based on ID. However, in my dataset the ID has multiple copies of duplicates and it is not a fixed number (copies = 0 to 9).

    My current dataset looks like this
    ID var1 var2 var3 var4 var5
    1 . 1 . 5 .
    1 2 . . . .
    1 . . . . 3
    2 1 . . . .
    2 . 3 . . .
    3 5 . . . .
    3 . 7 . . .
    3 . . 6 . .
    3 . . . 7 .

    Required output is
    ID var1 var2 var3 var4 var5
    1 2 1 . 5 3
    2 1 3 . . .
    3 5 7 6 7 .
    Last edited by Nikhil Kanakamedala; 21 May 2022, 01:31.

  • #2
    Nikhil:
    welcome to this forum.
    See -help collapse-.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      I agree with Carlo Lazzaro. The simplest collapse of all, using means, works with the data example.

      Note the use here of dataex (FAQ Advice #12).

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte(id var1 var2 var3 var4 var5)
      1 . 1 . 5 .
      1 2 . . . .
      1 . . . . 3
      2 1 . . . .
      2 . 3 . . .
      3 5 . . . .
      3 . 7 . . .
      3 . . 6 . .
      3 . . . 7 .
      end
      
      . collapse var? , by(id)
      
      . l
      
           +---------------------------------------+
           | id   var1   var2   var3   var4   var5 |
           |---------------------------------------|
        1. |  1      2      1      .      5      3 |
        2. |  2      1      3      .      .      . |
        3. |  3      5      7      6      7      . |
           +---------------------------------------+

      Comment


      • #4
        Thank you Carlo Lazzaro and Nick Cox. Worked like a charm
        Thanks also on the advice to use dataex. Will do from my next post.

        Comment

        Working...
        X