Hello
I am trying to run a for loop over a varlist where I want to replace an observation of a variable with the subsequent one if the value of the original observation is 0.
eg:
replace mv1=mv2 if mv1==0
replace mv2=0 if mv1==mv2
Now since I have mv1-mv24, I tried a for loop. Since I am new to for loop and STATA I didn't know exactly what to find and where. My code is below:
foreach var of varlist mv3-mv24 {
replace `var' = `var'[_n+1] if `var'==0
replace `var'[_n+1] = 0 if `var' = `var'[_n+1]
}
But I do not get what I want. Could someone help me?
Thank You
Sayli J
I am trying to run a for loop over a varlist where I want to replace an observation of a variable with the subsequent one if the value of the original observation is 0.
eg:
replace mv1=mv2 if mv1==0
replace mv2=0 if mv1==mv2
Now since I have mv1-mv24, I tried a for loop. Since I am new to for loop and STATA I didn't know exactly what to find and where. My code is below:
foreach var of varlist mv3-mv24 {
replace `var' = `var'[_n+1] if `var'==0
replace `var'[_n+1] = 0 if `var' = `var'[_n+1]
}
But I do not get what I want. Could someone help me?
Thank You
Sayli J
Comment