Announcement

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

  • Summing variable across different units by year

    Hi All,

    I have data that looks like the following:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(country year population)
    1 1980 100
    1 1981 150
    1 1982 200
    2 1980 200
    2 1981 300
    2 1982 300
    end
    ------------------ copy up to and including the previous line ------------------


    In the above, 1 and 2 represent country identifiers. What I wish to do, is for each year (1980, 1981 etc), I wish to create a new variable, which represents the sum of country 1 and 2's population. In particular, the final dataset I envision should looks something like:


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(var1 var2 var3)
    1 1980 100
    1 1981 150
    1 1982 200
    2 1980 200
    2 1981 300
    2 1982 300
    3 1980 300
    3 1981 450
    3 1982 500
    end


    In the above, 3 is a new identifier representing the combination of country 1 and 2. Reshaping the data and summing across rows would work, but that might be overkill for this particular operation. Any suggestions are most welcome!

    Best,
    CS

  • #2
    Code:
    help egen
    
    egen wanted = total(pop), by(country)

    Comment


    • #3
      Thanks a lot Nick Cox .

      Comment

      Working...
      X