Dear Statalists,

I am dealing with an unbalanced panel data for calculating the share holdings by a particular investing company. As shown from the table below, I would like to calculate the quarterly holding changes by investing company (id=1001) for each portfolio firm (e.g., a, b, c, d, e), and then use the difference times the stock price of each portfolio firm in the incumbent quarter.
year quarter investment company portfolio firm number of shares stock price of each holding firm
1996 1 1001 a 1000 3.33
1996 1 1001 b 1200 3.33
1996 1 1001 c 1300 3.33
1996 1 1001 d 1100 3.33
1996 1 1001 e 1050 3.33
1996 2 1001 a 1100 4.44
1996 2 1001 b 1100 4.44
1996 2 1001 c 1200 4.44
1996 2 1001 d 1400 4.44
1996 2 1001 e 0 4.44
1996 3 1001 a 900 5.55
1996 3 1001 b 1300 5.55
1996 3 1001 c 1200 5.55
1996 3 1001 d 1400 5.55
1996 4 1001 a 1200 6.66
1996 4 1001 b 1030 6.66
1996 4 1001 c 1000 6.66
1996 4 1001 d 1409 6.66
1997 1 1001 a 2000 7.77
1997 1 1001 b 1700 7.77
1997 1 1001 c 1344 7.77
1997 1 1001 d 1278 7.77
1997 2 1001 a 1900 8.88
1997 2 1001 b 2000 8.88
1997 2 1001 c 1300 8.88
1997 2 1001 d 700 8.88
For example, for portfolio firm a, firstly, I need to calculate the difference in holding shares by investing firm 1001 between the first and second quarter in 1996, which is 1100-1000=100, secondly, I will use such difference times the stock price of portfolio firm a in the second quarter of 1996, which is 100*4.44. This is just for one firm, while the same process should be performed for other portfolio firms. Similarly, for portfolio firm b, the equation should be (1100-1200)*4.4...

In general, the calculation equation could be:

(Nj, i, t - Nj, i, t-1)* Pj,t, where Nj,i,t is the number of shares holding by investing firm i in portfolio firm j in quarter t, Pj, t is the stock price for portfolio firm j in quarter t.

It would be much appreciated if some one can show me the stata codes in dealing with such issue.

Best,
Cong