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]

From |
Nick Cox <[email protected]> |

To |
"'[email protected]'" <[email protected]> |

Subject |
RE: st: Calculating cumulative value over several observations |

Date |
Mon, 13 Dec 2010 10:23:14 +0000 |

Note that my earlier sum(((Seller == `j') * Price) - ((Buyer == `j') * Price)) can naturally be reduced to sum(Price * ((Seller == `j') - (Buyer == `j'))) and a similar simplification can be applied to Clyde's independent solution. Nick [email protected] Clyde Schechter =============== I think this will get you what you want: levelsof buyer, local(buyers) levelsof seller, local(sellers) local participants: list buyers | sellers sort market period observation foreach p of local participants { by market period (observation): gen M`p' = sum(price*(seller==`p')) - sum(price*(buyer==`p')) by market period (observation): gen A`p' = sum(buyer == `p') - sum(seller == `p') } Note: If your buyers and sellers constitute the integers between 1 and P for some P, then you don't need to do the -levelsof- commands and you can replace the foreach loop with a forvalues p = 1/P loop. By the way, just out of curiosity, if everybody starts out with no money and no apples, how do they make these trades? Nick Cox ======== 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')) } Palan, Stefan 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? * * 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/

**References**:**RE: st: Calculating cumulative value over several observations***From:*"Clyde Schechter" <[email protected]>

- Prev by Date:
**Re: st: Different FE estimates** - Next by Date:
**st: Simple but baffling - problems merging files** - Previous by thread:
**RE: st: Calculating cumulative value over several observations** - Next by thread:
**st: Cut function** - Index(es):