How can I condition some replacement in the values of other variables in that same observation (not doing it one by one).
For example I have variables v1, v2, v3 and v4, with v4 just missing values. I need to replace v4=x, conditional in that v1, v2 and v3 are not equal to x in that observation.
The original problem is the following: I reshaped and then merged 2 datasets leading to a dataset that looks something like this:
And I would need to transform into something like this:
If there is a simple way to do this, then my original question is not pertinent anymore.
What I am doing to do the previous transformation is:
local i=1
local j=1
while `i' < 15 {
gen v`j'=.
if _merge==1 replace v`j'=v2_`j'
if _merge==2 replace v`j'=v1_`j'
local i=`i'+1
local j=`j'+1
}
local i=2
while `i' < 15 {
local j=1
while `j' < 10 {
local z = `i' - 1
replace v`i'=v2_`j' if _merge==3 & v`i'==. & v1==v1_1 & v`z'!=v2_`j' & `i'<=nm
replace v`i'=v1_`j' if _merge==3 & v`i'==. & v1==v2_1 & v`z'!=v1_`j' & `i'<=nm
local j=`j'+1
}
local i=`i'+1
}
The condition that is in bold checks that the new value is not equal to the one just before, for example, that the potential replacemente for V5 is not equal to V4. What I need is to change that for a condition that checks that the potential replacement for V5 (v*_j in the code) is not equal no all the rest of the V* variables for that observation. With that, the transformation should work without problems. Is this possible?
Thank you!
For example I have variables v1, v2, v3 and v4, with v4 just missing values. I need to replace v4=x, conditional in that v1, v2 and v3 are not equal to x in that observation.
The original problem is the following: I reshaped and then merged 2 datasets leading to a dataset that looks something like this:
ID | v1_1 | v1_2 | v2_3 | non-missing1 | v2_1 | v2_2 | v2_3 | non-missing2 |
1 | 1 | 2 | . | 2 | 8 | . | . | 1 |
2 | 3 | . | . | 1 | 9 | . | . | 1 |
3 | 4 | 5 | . | 2 | 10 | 11 | . | 2 |
4 | 6 | 7 | . | 2 | 12 | 13 | 14 | 3 |
ID | v1 | v2 | v3 | v4 | v5 |
1 | 1 | 2 | 8 | . | . |
2 | 3 | 9 | . | . | . |
3 | 4 | 5 | 10 | 11 | . |
4 | 6 | 7 | 12 | 13 | 14 |
What I am doing to do the previous transformation is:
local i=1
local j=1
while `i' < 15 {
gen v`j'=.
if _merge==1 replace v`j'=v2_`j'
if _merge==2 replace v`j'=v1_`j'
local i=`i'+1
local j=`j'+1
}
local i=2
while `i' < 15 {
local j=1
while `j' < 10 {
local z = `i' - 1
replace v`i'=v2_`j' if _merge==3 & v`i'==. & v1==v1_1 & v`z'!=v2_`j' & `i'<=nm
replace v`i'=v1_`j' if _merge==3 & v`i'==. & v1==v2_1 & v`z'!=v1_`j' & `i'<=nm
local j=`j'+1
}
local i=`i'+1
}
The condition that is in bold checks that the new value is not equal to the one just before, for example, that the potential replacemente for V5 is not equal to V4. What I need is to change that for a condition that checks that the potential replacement for V5 (v*_j in the code) is not equal no all the rest of the V* variables for that observation. With that, the transformation should work without problems. Is this possible?
Thank you!
Comment