Announcement

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

  • Adding duplicates together

    I have two data sets.

    data set a with group level data for variables x, y, and group_id.

    data set b with individual level data for variables a, b and group_id. This data has duplicates of group_id. I want to add together var_a if group_id is the same and add together var_b if group_id is the same. To transform individual level data into group level data so I can merge it with data set a.

    current data set b
    group_id var_a var_b
    1 100 60
    1 50 10
    2 30 20
    2 10 70
    3 110 130
    I want it to look like:
    group_id var_a var_b
    1 150 70
    2 40 90
    3 110 130
    I have tried to gen and egen new variables but have not been able to get anything to work. Any code or even the command to look at would be appreciated.

  • #2
    Code:
    collapse (sum) var_* , by (group_id)

    Comment


    • #3
      After struggling for too long I got it 10 minutes after posting this.

      The code I used is:

      egen var_a2=total(var_a), by (group_id)
      egen var_b2=total(var_b), by (group_id)
      drop var_a
      drop var_b

      Comment

      Working...
      X