Hi,
Below is an example data:
first, I want to flag observations which change locations between two consecutive rounds. I use the following code for this:
next, I want to harmonize the locations across rounds for each id and make it equal to that in the first available round
For this, i try the following code:
So I finally have the following data:
here, only for id=4, it is the location of round 5 that has been repeated across all rounds, while for all other ids, it is the location of the first round. I'm confused why this is happening and would appreciate any solution.
Thanks,
Below is an example data:
Code:
* Example generated by -dataex-. For more info, type help dataex clear input float(id round location) 1 1 1 1 2 1 1 3 1 1 4 . 1 5 2 2 1 2 2 2 2 2 4 2 2 5 3 3 1 4 3 2 3 3 3 . 3 5 4 4 1 2 4 2 2 4 5 5 end
Code:
by id:gen flag=location[_n+1]!=location
For this, i try the following code:
Code:
gen location2 = location capture noisily forval r = 1/5 { bysort id : egen value`r' = mean(cond(round == `r', location, .)) } replace location2 = cond(value3 < ., value3, cond(value4 < ., value4, value5)) if round >= 1 order location2,a(location)
Code:
* Example generated by -dataex-. For more info, type help dataex clear input float(id round location location2 flag value1 value2 value3 value4 value5) 1 1 1 1 0 1 1 1 . 2 1 2 1 1 0 1 1 1 . 2 1 3 1 1 1 1 1 1 . 2 1 4 . 1 1 1 1 1 . 2 1 5 2 1 1 1 1 1 . 2 2 1 2 2 0 2 2 . 2 3 2 2 2 2 0 2 2 . 2 3 2 4 2 2 1 2 2 . 2 3 2 5 3 2 1 2 2 . 2 3 3 1 4 4 1 4 3 . . 4 3 2 3 4 1 4 3 . . 4 3 3 . 4 1 4 3 . . 4 3 5 4 4 1 4 3 . . 4 4 1 2 5 0 2 2 . . 5 4 2 2 5 1 2 2 . . 5 4 5 5 5 1 2 2 . . 5 end
Thanks,
Comment