Dear All,

Task: Variable X has a lot of missing values, suppose the first three values are missing, and for each non-missing values, say, the fifth observation of X, X[5] is non-missing, I want to replace X[3] by X[5], and such operation holds for all the non-missing values.

My inefficient solution looks like the following:

1. Identify the location of the non-missing value of X
gen index = _n if !missing(X)
list index if !missing(index)

e.g., it gives two numbers, say 34, 44 , then I key-in the number 32 and 42 in the following loop

2. foreach a of numlist 32 42 {
replace X = X[`a'+2] in `a'
}

I am wondering how to avoid keying in the numbers 32 and 42 in the above foreach loop since if numlist containing a lot of numbers, this obviously becomes infeasible

Thank you very much for your help.

Dong