This is what my dataset looks like:
Code:
input float(firmid year firm_prod) long productid double value
1 2014  5 92029000                  0
1 2015  3 85444110                  0
1 2015  6 92079000                  0
1 2015  2 85182100                  0
1 2015  4 92021000                  0
1 2015  1 42029200                  0
1 2015  5 92029000                  0
1 2015  7 92099200                  0
1 2016  1 42029200 12.706065809481624
1 2016  2 85182100   8.36822903827628
1 2016  6 92079000 10.515777110135918
1 2016  7 92099200   5.37989735354046
1 2016  5 92029000 10.767600388351703
1 2016  3 85444110  8.797699580118923
1 2016  4 92021000  9.025335217553026
2 2012 18 94039000                  0
2 2012 24 95039000                  0
2 2012 11 87150000                  0
2 2012 14 94017900                  0
2 2012 13 94017190                  0
2 2012 15 94018090                  0
2 2012 20 94049040                  0
2 2012 22 95010000                  0
2 2012 17 94032000                  0
I want to calculate on average how many new products a firm sells each year. firm_prod is the group value for each firmid-productid combination. I was able to count how many new products a firm adds in a year, but this does not exclude new entrant.
Code:
xtset firm_prod year, yearly
gen add = .
replace add = 0 if value== 0 & L.value == 0
replace add = 1 if value >0 & L.value == 0
egen total_added = total(add), by(firmid year)
For example, for firm 1 in the example above, they only enter in 2016 with 7 products, so my code would count this as 7 new products for firm 1. However, I want to count how many new products an existing firm addes in the following year. This would exclude firms that are total new entrants in each year.

I have been trying to figure this out, but I'm not sure how to get around it. Thank you for your help.