*! version 3.0.0 06/01/92 running median, copy-on. program define nlsm_m /* # varname newvar */ version 3.0 local span `1' confirm integer number `span' if `span'<1 { exit 198 } local v "`2'" local generat "`3'" if "`generat'"=="" { exit 198 } local type : type `v' tempvar res time quietly { gen long `time'=_n gen `type' `res'=`v' /* copy on */ if `span'==1 { rename `res' `generat' exit } local h=int(`span'/2) if (`h'*2!=`span') { /* odd */ local s 3 local j0 2 } else { local s 2 local j0 1 local h=`h'-1 } local j1=_N-1 while `j0'<=`h' { nlsm_mu `s' `v' `res' `time' `j0' `j0' nlsm_mu `s' `v' `res' `time' `j1' `j1' local j0=`j0'+1 local j1=`j1'-1 local s=`s'+2 } nlsm_mu `span' `v' `res' `time' `j0' `j1' } rename `res' `generat' end exit Shorter-span logic span=3 span=5 span=7 span=2 span=4 span=6 h=1 h=2 h=3 h=1 h=2 h=3 --------------------------------------------------------------- 1. ->1 ->1 ->1 1.5 1.5(*2) 1.5(*2) 2. 2 2(*3) 2(*3) 2.5 2.5 2.5(*4) 3. 3 3 3(*5) 3.5 3.5 3.5 4. 4 4 4 4.5 4.5 4.5 5. 6. 6 6 6 6.5 6.5 6.5 7. 7 7 7(*5) 7.5 7.5 7.5(*4) 8. 8 8(*3) 8(*3) 8.5 8.5(*2) 8.5(*2) 9. ->9 ->9 ->9 ->9 ->9 ->9 Thus, for odd: fill in j=2,..h and N-1 ... For even, fill in j=1,..h-1 and N_1 ...