I have the variables below:
rid revenue pid in_stock
46532 2085405 1013 1
58914 3964121 1021 1
I would like to reshape them as follows:
rid revenue pid in_stock
46532 2085405 1013 1
46532 2085405 1021 0
58914 3964121 1013 0
58914 3964121 1021 1
How can I do this in Stata?
This is too long and too awkward to work well as a comment on @Pearly Spencer's answer. It seems to me that this can be done a little more simply. fillin
will do most of the work and there is no need to use save
, expand
or joinby
.
clear
input rid revenue pid in_stock
46532 2085405 1013 1
58914 3964121 1021 1
end
fillin rid pid
replace in_stock = 0 if _fillin
drop _fillin
bysort rid (revenue) : replace revenue = revenue[1]
list, sepby(rid)
+-----------------------------------+
| rid revenue pid in_stock |
|-----------------------------------|
1. | 46532 2085405 1013 1 |
2. | 46532 2085405 1021 0 |
|-----------------------------------|
3. | 58914 3964121 1021 1 |
4. | 58914 3964121 1013 0 |
+-----------------------------------+