I have the above example data regarding selling or buying assets. I would like to calculate the number of holding months of the portfolio a and b indicated in id (i.e. how many months did the individual hold the asset before fully sold since the first purchase). The variable "reset_zero" equals 1 if the asset at that time was sold completely, so the number of holding months has to be reset and recalculated. Basically, the number "1" in the variable "reset_zero" creates a circle. I tried different ways and still cannot generate desired results as the variable "holding_month", which was created manually. Any suggestions are highly appreciated. Thank you very much.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str10 id float(trd_month1 month_id) byte i_buy_sell float(reset_zero holding_month) "a" 695 284 1 . 0 "a" 696 696 . . 1 "a" 697 697 . . 2 "a" 698 698 . . 3 "a" 699 699 . . 4 "a" 700 700 . . 5 "a" 702 290 0 1 7 "a" 701 290 1 . 0 "a" 702 291 0 1 1 "a" 705 294 1 . 0 "a" 705 294 1 . 0 "a" 706 295 0 . 1 "a" 707 707 . . 2 "a" 708 297 0 1 3 "b" 662 251 1 . 0 "b" 662 251 0 1 0 "b" 663 252 1 . 0 "b" 663 252 0 1 0 "b" 663 252 1 . 0 "b" 663 252 0 1 0 "b" 663 252 1 . 0 "b" 664 253 0 1 1 end format %tm trd_month1 label values i_buy_sell buysell label def buysell 0 "sell", modify label def buysell 1 "buy", modify
Comment