Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st: RE: Calculating cumulative value over several observations
From 
 
Nick Cox <[email protected]> 
To 
 
"'[email protected]'" <[email protected]> 
Subject 
 
st: RE: Calculating cumulative value over several observations 
Date 
 
Fri, 10 Dec 2010 17:33:43 +0000 
forval j = 1/6 {
	bysort Market Period (Obs): gen M`j' = sum(((Seller == `j') * Price) - ((Buyer == `j') * Price)) 
	by Market Period (Obs): gen A`j' = sum(((Buyer == `j') - (Seller == `j'))
} 
Nick 
[email protected] 
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Palan, Stefan ([email protected])
Sent: 10 December 2010 17:19
To: [email protected]
Subject: st: Calculating cumulative value over several observations
Hi everybody,
In my dataset every observation is one trade between a buyer and a seller. Each person is uniquely identified by an ID and can buy and sell (so the same ID will be a buyer in one observation and a seller in another observation). In fact, some example observations would look like this:
Observation Market Period Price Seller Buyer
1           1      1      100   4      2
2           1      1      105   4      6
3           1      1      103   2      5
4           1      1      107   6      4
5           1      2      89    3      1
6           1      2      90    4      3
7           2      1      79    3      2
Each transaction here is for, say, 1 apple. Now I would like to calculate, observation by observation, each person's holdings of money and apples, resetting with every new market and period. The result could look as follows (assuming every individual starts out with no money and no apples, M# stands for money of person #, A# stands for apples of person #):
Obs Market Period M1    M2    M3    M4    M5    M6    A1  A2  A3  A4  A5  A6
1   1      1      0     -100  0     100   0     0     0   1   0   -1  0   0
2   1      1      0     -100  0     205   0     -105  0   1   0   -2  0   1
3   1      1      0     3     0     205   -103  -105  0   0   0   -2  1   1
4   1      1      0     3     0     98    -103  2     0   0   0   -1  1   0
5   1      2      -89   0     89    0     0     0     1   0   -1  0   0   0
6   1      2      -89   0     -1    90    0     0     1   0   0   -1  0   0
7   2      1      0     -79   79    0     0     0     0   1   -1  0   0   0
Unfortunately, I don't know how to efficiently add up the information in this way. Can somebody please help me with reshaping my data to get this output?
Cheers,
Stefan.
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/