Hi statlist,

as Prof suggested me I am reading the even pdf command but I am still quite confused. I have the following dataset:

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input double idfirm float Year double idproduct
1 2008  3051
1 2008 12012
1 2008  8687
1 2008  4088
1 2009  8687
1 2009  4088
1 2009 12012
1 2009  3051
1 2010  8687
1 2010 16087
1 2010  3051
1 2010  4088
1 2010 12012
1 2011 18347
1 2011  4088
1 2011 12012
1 2011  3051
1 2011  8687
1 2011 10080
1 2011 16087
1 2012  8687
1 2012  4088
1 2012  7657
1 2012  3051
1 2012 16087
1 2012  6051
1 2012 10080
1 2012 12012
1 2012  7193
1 2013 10080
1 2013  6051
1 2013  3051
1 2013 12012
1 2013 11192
1 2013  7193
1 2013 11757
1 2013  7657
1 2013  4088
1 2013 16087
1 2013   287
1 2013 21124
1 2013  8687
1 2013  1474
1 2014 12012
1 2014  8087
1 2014 11757
1 2014  3026
1 2014  8687
1 2014   287
1 2014 21124
1 2014  7193
1 2014 19783
1 2014 10237
1 2014 16087
1 2014 10080
1 2014  3051
1 2014  1474
1 2014  4088
1 2014  7657
1 2014  6051
1 2014 21428
1 2014 11192
1 2015  3026
1 2015 12012
1 2015 10237
1 2015  4088
1 2015   287
1 2015 21428
1 2015  8087
1 2015 11192
1 2015 11757
1 2015 19783
1 2015  1474
1 2015  7193
1 2015  3051
1 2015 21124
1 2015 16087
1 2015  6051
1 2015 10080
1 2015  7657
2 2004  2669
2 2004  4632
2 2004 19459
2 2004 19458
2 2004  1690
2 2004  1691
2 2005  1691
2 2005  4632
2 2005  1690
2 2005 19459
2 2005 19458
2 2005  2669
2 2006  1690
2 2006  4632
2 2006 19458
2 2006 19459
2 2006  2669
2 2006  1691
2 2007  4632
2 2007 19459
end
What I would like to do is to build up a variable "counter" taking on 1 if a product is lost in a year, that is if it is observed in one year and it disappears the next year. So the point is that I do not know how to do it because if the product disappears the successive year, I cannot flag it with a 1. I can just flag it in the year before it disappears (e.g. product 18347 in 2011 disappears in 2012; actually I would like to flag product 18347 in 2012 because in a successive stage I could collapse (sum) counter, by(idfirm Year) in order to obtain the number of lost products in 2012, but I cannot because indeed it disappears). How can I do it? The idea that I have is to invert the flag, that is flag with 1 the products that do not disappear from one year to the other and then make the difference between the total number of products in a year and the flagged values...but don't know...

Thank you very much,

Federico