Announcement

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

  • Find sum for each year,id in panel data

    Hello Stata Xperts!

    Need some help.
    I have the following panel data.
    Year = year
    Id = Identifier of the company
    Emp = Total number of employees in the firm

    I need to find the total number of employees for each year. i.e. in 1990, there are total 100 employees, while in 1991, there are total 250 employees.

    Code:
    clear
    input float(Year Id Emp)
    1990 1 25
    1990 1 25
    1990 1 25
    1990 2 75
    1990 2 75
    1991 1 50
    1991 1 50
    1991 2 200
    1991 2 200
    1991 2 200
    I would like to find the sum of the number of employees (emp) for each year. i.e. for 1990, the total number of employees is 100 (25 + 75). Similarly, for 1991, the total number of employees is 250 (50 + 200). Thanks for your help, J

  • #2
    Your data structure is a bit bizarre, because you have all these redundant duplicate observations. The simplest approach would be:

    Code:
    duplicates drop 
    by Year, sort: egen total_employees = total(Emp)
    On the assumption that there is some reason why you actually need to retain all those duplicate observations, an alternate approach would be:

    Code:
    egen flag = tag(Year Id)
    by Year, sort: egen total_employees = total(cond(flag, Emp, .))

    Comment


    • #3
      Thanks Clyde. Actually I do need those observations. Your code works. Thanks a lot!

      Comment

      Working...
      X