Hello all,


I have a "date" variable and an "ID" variable for a number of observations. I want to generate a new variable ("desired variable"), the value of which represents the number of previous occurrences of the corresponding "ID" within exactly one year prior to the date of that observation. An example of what I want to get is shown in the table below where I have manually calculated the values of the "desired variable" to demonstrate what I wish to output.
ID Date Desired variable
1 01/08/2003 0
1 05/09/2003 1
1 06/03/2005 0
2 13/05/2010 0
2 01/05/2015 0
3 03/09/2015 1
4 01/01/1999 0
4 01/02/1999 1
4 05/08/1999 2
4 10/10/1999 3
I would be very grateful for your help with this.