Hi,

I have the following panel data (gvkey fyear). Loss is a dummy variable that equals 1 if ib <0.
I want to construct a new variable (Loss%) which is the cumulative percentage of sample years that the firm reported a loss. Does anyone know how to do this?

Thank you in advance!


Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str6 gvkey double(fyear ib) float Loss
"001000" 1965   -.197 1
"001000" 1966    .201 0
"001000" 1967    -.09 1
"001000" 1968    .347 0
"001000" 1969   1.835 0
"001000" 1970   1.878 0
"001000" 1971    .138 0
"001000" 1972   1.554 0
"001000" 1973   1.863 0
"001000" 1974   1.555 0
"001000" 1975   2.284 0
"001000" 1976   3.434 0
"001000" 1977   1.928 0
"001001" 1978       . .
"001001" 1979       . .
"001001" 1980       . .
"001001" 1981       . .
"001001" 1982       . .
"001001" 1983   1.135 0
"001001" 1984   1.138 0
"001001" 1985   2.576 0
"001002" 1965      .4 0
"001002" 1966     .19 0
"001002" 1967     .72 0
"001002" 1968    .903 0
"001002" 1969    .092 0
"001002" 1970    .487 0
"001002" 1971    .796 0
"001002" 1972    .898 0
"001003" 1980       . .
"001003" 1981       . .
"001003" 1982    .929 0
"001003" 1983    1.05 0
"001003" 1984    .387 0
"001003" 1985    .236 0
"001003" 1986    .793 0
"001003" 1987   -.525 1
"001003" 1988  -7.838 1
"001003" 1989  -2.554 1
"001004" 1965     .35 0
"001004" 1966    .279 0
"001004" 1967    .015 0
"001004" 1968    .354 0
"001004" 1969    .303 0
"001004" 1970    .454 0
"001004" 1971    .854 0
"001004" 1972   1.502 0
"001004" 1973   2.105 0
"001004" 1974   1.866 0
"001004" 1975    1.86 0
"001004" 1976   1.801 0
"001004" 1977   2.015 0
"001004" 1978   3.005 0
"001004" 1979   3.721 0
"001004" 1980   3.606 0
"001004" 1981   1.225 0
"001004" 1982   2.795 0
"001004" 1983   4.487 0
"001004" 1984   9.071 0
"001004" 1985  11.506 0
"001004" 1986  15.361 0
"001004" 1987  21.233 0
"001004" 1988  24.814 0
"001004" 1989  25.655 0
"001004" 1990  14.801 0
"001004" 1991   10.02 0
"001004" 1992    .283 0
"001004" 1993   9.484 0
"001004" 1994  10.463 0
"001004" 1995  16.012 0
"001004" 1996  23.025 0
"001004" 1997  35.657 0
"001004" 1998  41.671 0
"001004" 1999  35.163 0
"001004" 2000  18.531 0
"001004" 2001 -58.939 1
"001004" 2002  -12.41 1
"001004" 2003   3.504 0
"001004" 2004  18.572 0
"001004" 2005  35.163 0
"001004" 2006  59.447 0
"001004" 2007  75.745 0
"001004" 2008    80.6 0
"001004" 2009  44.628 0
"001004" 2010  73.139 0
"001004" 2011  67.723 0
"001004" 2012      55 0
"001004" 2013    72.9 0
"001004" 2014   -54.5 1
"001004" 2015    40.5 0
"001004" 2016    50.2 0
"001004" 2017    73.7 0
"001005" 1973       . .
"001005" 1974   -.016 1
"001005" 1975    .293 0
"001005" 1976    .388 0
"001005" 1977    .289 0
"001005" 1978    .199 0
"001005" 1979    .452 0
"001005" 1980    .926 0
end