I have a dataset with: i) date, ii) firm, and iii) price. Sometimes price is missing. I need the previous price when there was a (nonmissing) price but am having difficulties locating it as the windows of missing obs are random. I tried searching and found some related posts, but could not get their code to work. Also, it was never the same exact situation as mine.
On the days when there is a price, i would like to include on the same row/observation the last nonmissing previous price as well as the gap (in dates) from the current and the previous price. And do all of this within group (firm).
The solution would look like:
Thank you in advance.
Code:
clear input float date float firm float price 1 1 4 2 1 . 3 1 6 4 1 . 5 1 . 6 1 . 7 1 4 8 1 . 9 1 . 10 1 6 1 2 . 2 2 5 3 2 4 4 2 . 5 2 . 6 2 . 7 2 6 8 2 . 9 2 . 10 2 . end
The solution would look like:
Code:
clear input float date float firm float price float lastprice float dategap 1 1 4 . . 2 1 . . . 3 1 6 4 2 4 1 . . . 5 1 . . . 6 1 . . . 7 1 4 6 4 8 1 . . . 9 1 . . . 10 1 6 6 3 1 2 . . . 2 2 5 . . 3 2 4 5 1 4 2 . . . 5 2 . . . 6 2 . . . 7 2 6 4 4 8 2 . . . 9 2 3 6 2 10 2 . . . end
Comment