Dear reader,
Currently, I have a dataset with the variables execid (executive id), cusip (firm id) and years (fyear)
I would like to count the amount of executives of a firm in a given year.
I want to create a new variable that counts the differcent execid. Current code:
This code is however wrong because its counts the distinct execid over ALL years from 2002 to 2009.
I need stata to calculate per year which, from the example, would result in:
For cusip 000360 in 2006: 5
For cusip 000360 in 2007: 5
For cusip 000360 in 2008: 5
For cusip 000360 in 2009: 4
I really hope someone knows the answer.
Regards,
Nicole
Currently, I have a dataset with the variables execid (executive id), cusip (firm id) and years (fyear)
I would like to count the amount of executives of a firm in a given year.
PHP Code:
Cusip execid fyear
000360 36419 2006
000360 36416 2006
000360 36417 2006
000360 36418 2006
000360 36415 2006
000360 36418 2007
000360 36416 2007
000360 36419 2007
000360 36415 2007
000360 36417 2007
000360 36418 2008
000360 36417 2008
000360 36419 2008
000360 36415 2008
000360 36416 2008
000360 36419 2009
000360 36416 2009
000360 36417 2009
000360 36418 2009
Code:
by cusip execid (fyear), sort: gen byte exec_count = 1 if _n == 1 & inrange(fyear, 2002, 2009) by cusip (execid fyear): replace exec_count = sum(exec_count) by cusip (exec_count), sort: replace exec_count = exec_count[_N]
I need stata to calculate per year which, from the example, would result in:
For cusip 000360 in 2006: 5
For cusip 000360 in 2007: 5
For cusip 000360 in 2008: 5
For cusip 000360 in 2009: 4
I really hope someone knows the answer.
Regards,
Nicole
Comment