Hello Everyone,

I use Stata 15.

The table below shows customer ID, year, and the value of transactions each customer did with two brands (brand1, and brand2).

I need to calculate the total value of transactions each customer did with each brand in each two consecutive years starting in 1990 (i.e., 1990-1991, 1992-1993, 1994-1995, and so on). That said, transactions in year 1991 should not be added to transactions in year 1992.

Then, I need to calculate the difference between the total transactions with brand1 and brand2, for each two consecutive years, for each customer (for example, transactions with brand1 in 1990-1991 minus transactions with brand2 in 1990-1991)

Thank you very much for your help.

id year brand1 brand2
1 1989 400 0
1 1990 100 50
1 1990 100 0
1 1991 100 0
1 2001 100 0
2 2000 250 0
2 2000 0 300
2 2000 0 690
2 2001 0 230
3 2009 890 0
3 2010 200 0
3 2011 0 2000
4 2013 120 0
4 2015 450 0
4 2015 400 230
4 2015 450 1200
4 2016 110 0