mimirose
mimirose

Reputation: 23

Stata panel data - count people (not observations) in a group

I have a panel dataset in Stata that contains payroll data for 261 employers over two years. Each agency has a unique ID variable, as does each employee. Each row of data is a pay period. I'm trying to figure out how to count the number of employees for each agency. I was easily able to count the number of pay periods per employee using by employee: gen pp_id = _n but this does not work for counting employees within an agency.

I've tried using egen employeecount = count(employee), by(agency), but that seems to add up the value of the employee IDs rather than counting the number (so an agency with employees 5, 15, and 20 would have employeecount 40 instead of 3).

Is there a solution for this? Is there another way entirely that I should be approaching this? Thank you!

Upvotes: 1

Views: 7344

Answers (2)

Nick Cox
Nick Cox

Reputation: 37233

There is a review of this territory in Stata in http://www.stata-journal.com/sjpdf.html?articlenum=dm0042

What @Dimitriy V. Masterov recommends is fine, but you can do that in two steps without installing anything extra.

egen tag = tag(employee agency) 
egen employeecount = total(tag), by(agency)

For what is happening here, and much else, the paper cited gives the full story.

Upvotes: 3

dimitriy
dimitriy

Reputation: 9460

Try

egen employeecount = nvals(employee), by(agency)

to get the number of distinct values of employee within agency. This is documented under help egenmore, which you will have to install.

Upvotes: 3

Related Questions