Hi everyone,
I have a Problem concerning the following dataset:
I want to create a table that looks the following:
In principle I want to create two new variables.
The first variable sell_price shows the last "Value" when Action=sell. What is important (and for me difficult to code) that it only shows the value where Action=buy for the second time in each Name. As you can see for Name=1 there is Action=buy only once. Thus, no change needs to be made to the variable. For Name=2 there is Action=buy appearing multiple times. For each time except for the first time I would like to have there the Value for Action=sell.
The second variable is supposed to be Buy_Again=1 from the second time on there is Action=buy until the next time there is Action=sell for each name. For the third, fourth (… etc.) time that there is Action=buy this system is supposed to continue
I hope you could understand what I wanted to explain. It would be great if anyone could help me on that.
Thanks a lot!
I have a Problem concerning the following dataset:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float Date str19 Action str12 Name float Value 19348 "buy" "1" 3.272727 19381 "" "1" 3.636909 19439 "sell" "1" 3.409091 19530 "" "1" 3.3163636 19544 "sell" "1" 3.231818 19563 "" "1" 3.661818 19348 "buy" "2" 2.4194546 19381 "" "2" 2.448636 19439 "sell" "2" 2.9858184 19530 "buy" "2" 3.305909 19696 "" "2" 3.93 19725 "sell" "2" 4.2650003 20275 "buy" "2" 4.828182 20402 "" "2" 6.074545 20471 "buy" "2" 6.119091 end format %td Date
I want to create a table that looks the following:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float Date str19 Action str12 Name float(Value sell_price Buy_Again) 19348 "buy" "1" 3.272727 . . 19381 "" "1" 3.636909 . . 19439 "sell" "1" 3.409091 . . 19530 "" "1" 3.3163636 . . 19544 "sell" "1" 3.231818 . . 19563 "" "1" 3.661818 . . 19348 "buy" "2" 2.4194546 . . 19381 "" "2" 2.448636 . . 19439 "sell" "2" 2.9858184 . . 19530 "buy" "2" 3.305909 2.9858184 1 19696 "" "2" 3.93 . 1 19725 "sell" "2" 4.2650003 . . 20275 "buy" "2" 4.828182 4.2650003 1 20402 "" "2" 6.074545 . 1 20471 "buy" "2" 6.119091 4.2650003 1 end format %td Date
The first variable sell_price shows the last "Value" when Action=sell. What is important (and for me difficult to code) that it only shows the value where Action=buy for the second time in each Name. As you can see for Name=1 there is Action=buy only once. Thus, no change needs to be made to the variable. For Name=2 there is Action=buy appearing multiple times. For each time except for the first time I would like to have there the Value for Action=sell.
The second variable is supposed to be Buy_Again=1 from the second time on there is Action=buy until the next time there is Action=sell for each name. For the third, fourth (… etc.) time that there is Action=buy this system is supposed to continue
I hope you could understand what I wanted to explain. It would be great if anyone could help me on that.
Thanks a lot!
Comment