Hi everyone, I have investors' portfolio dataset 1 as follows. id represents different investors; stock is the name of the stocks; share is the number of shares s/he has; buy_sell is the trading directions. The original dataset has more than 1000 investors and different stocks in more than 3 years. 
Now I would like to merge the dataset 1 to another dataset 2, which has the price for each stock over the years as time series.
The logic is, for each id in dataset 1, I would like to get each stock's price from dataset 2 after the investor bought the stock. Even if the person sold the stock at the month x or the datapoint in dataset 1 stopped at month x, I still would like to get stock's price in the next 12 months from dataset 2.So the merged data for investor A should look like this:
Not sure if it is possible to do that in Stata. If anyone could help, it would be great. Thank you very much in advance.
Dataset 1:
Dataset 2:
Now I would like to merge the dataset 1 to another dataset 2, which has the price for each stock over the years as time series.
The logic is, for each id in dataset 1, I would like to get each stock's price from dataset 2 after the investor bought the stock. Even if the person sold the stock at the month x or the datapoint in dataset 1 stopped at month x, I still would like to get stock's price in the next 12 months from dataset 2.So the merged data for investor A should look like this:
Not sure if it is possible to do that in Stata. If anyone could help, it would be great. Thank you very much in advance.
Dataset 1:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str9 id str12 stock float(month share buy_sell) "A" "Apple" 688 180 1 "A" "Apple" 689 180 . "A" "Apple" 690 . 0 "A" "Amazon" 697 10 1 "A" "Amazon" 698 10 . "A" "Amazon" 699 10 . "B" "Apple" 682 12 1 "B" "Apple" 683 12 . "B" "Apple" 684 12 . "B" "Apple" 685 . 0 "C" "Apple" 688 100 1 "C" "Apple" 689 100 . "C" "Apple" 690 . 0 "C" "Amazon" 699 20 1 "C" "Amazon" 700 . 0 "C" "Amazon" 701 20 1 "C" "Amazon" 702 . 0 end label values buy_sell buy_sell1 label def buy_sell1 0 "sell", modify label def buy_sell1 1 "buy", modify format %tm month
Dataset 2:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str12 stock float(month price) "Apple" 682 10 "Apple" 683 11 "Apple" 684 12 "Apple" 685 13 "Apple" 686 11 "Apple" 687 12 "Apple" 688 11 "Apple" 689 11 "Apple" 690 2 "Apple" 691 3 "Apple" 692 4 "Apple" 693 5 "Apple" 694 22 "Apple" 695 11 "Apple" 696 11 "Apple" 697 22 "Apple" 698 11 "Apple" 699 33 "Apple" 700 22 "Apple" 701 11 "Apple" 702 11 "Amazon" 697 3 "Amazon" 698 4 "Amazon" 699 2 "Amazon" 700 1 "Amazon" 701 4 "Amazon" 702 2 "Amazon" 703 5 "Amazon" 704 3 "Amazon" 705 6 "Amazon" 706 3 "Amazon" 707 6 "Amazon" 708 3 "Amazon" 709 6 "Amazon" 710 4 "Amazon" 711 6 "Amazon" 712 4 "Amazon" 713 6 "Amazon" 714 4 end format %tm month
Comment