Dear Statalists,

my dataset includes company IDs and patents the companies invented per year. Each line is a patent invented in a certain year by a certain company, so that there might be several lines per company/year. I am struggeling with the running sum of the number of patents (= number of obs.) per company for the last 3 years.

In my example, for 1994 I would like to have a 2 as in that year two patents have been invented and there are no previous years for that company. For 1995, I would like to have 8 (6 from 1995 and 2 from 1994). For 1996 its 11, for 1997 its 12 (1994 drops out) and so on...

Any ideas? Thanks in advance!

I am using Stata MP 15.0.


Code:
clear
input long permno float grant_year
10016 1994
10016 1994
10016 1995
10016 1995
10016 1995
10016 1995
10016 1995
10016 1995
10016 1996
10016 1996
10016 1996
10016 1997
10016 1997
10016 1997
10016 1998
10016 1998
10016 1998
10016 1998
10016 1998
10016 1998
10016 1998
10016 1998
end