Friday, January 7, 2022

How to replace value of a variable in one round with its value in a previous round ?

Hi all,

Please consider the following sample data

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(id round location)
1 1 1
1 2 1
1 3 1
2 1 2
2 2 2
2 3 4
3 1 3
3 2 4
3 3 3
end
In this panel data, location for ids 2 and 3 are not consistent across rounds. I want to make these locations consistent across rounds and equal to the location in round 1. So, location for id 2 would be 2 in all rounds, and for id 3 it would be 3 for all rounds.

I tried
Code:
sort id round
by id: replace location=location[_n-1]|location[_n-2] if location!=location[_n-1]|location!=location[_n-2]
but this simply converted all the locations to 1, which is the first location of id 1.


Would appreciate if someone could suggest how to go about this problem.

Thanks

No comments:

Post a Comment