Announcement

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

  • Sum across observations

    I have a dataset with regions and an id for a specific sector. As an example I have

    region id x1
    1 2361 6
    1 2362 10
    1 2363 11
    1 2364 4
    2 2361 8
    2 2362 23
    2 2363 6
    2 2364 2

    I need to sum 2361+2362 for each region and keep the id code of 2361 so that
    region id x1
    1 2361 16
    1 2363 11
    1 2364 4
    2 2361 31
    2 2363 6
    2 2364 2

    I found some previous posts about how to do it, but they seem to be a bit too complicated to do for all the variables. I need to do it for all 75 variables on the dataset.

  • #2
    Here's one way of doing this

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte region int id byte x1
    1 2361 6 
    1 2362 10 
    1 2363 11 
    1 2364 4 
    2 2361 8 
    2 2362 23 
    2 2363 6 
    2 2364 2 
    end
    
    * verify assumptions about the data
    isid region id, sort
    
    * group both 2361 2362 codes together
    replace id = 2361 if id == 2362
    
    * reduce to one obs per region id
    collapse (sum) x1, by(region id)
    
    list, sepby(region) noobs

    Comment


    • #3
      That worked. Thank you

      Comment

      Working...
      X