*! srecode--recode a variable in uniform "steps" dm25: STB-24 *! version 1.0 Sean Becketti December 1994 program define srecode version 3.1 local mv 9897969 /* magic number; overcomes parse limitation */ local varlist "req new max(1)" local exp "req nopre" local if "opt pre" local in "opt pre" local options "MAx(real -`mv') MIDpoint MIn(real `mv') Step(real 1)" parse "`*'" local s "`step'" /* s is the step size */ if `s'<=0 { noi di in re "step size must be > 0" exit 98 } tempvar touse x gen double `x' = `exp' `if' `in' mark `touse' `if' `in' markout `touse' `x' _crcnuse `touse' local inuse "in $S_3/$S_4" if $S_2 { local ifuse "if `touse'" local anduse "& `touse'" } if (`min'==`mv') | (`max'==-`mv') { qui sum `x' `ifuse' `inuse' if `min'== `mv' { local min = _result(5) } if `max'==-`mv' { local max = _result(6) } } if `min'>`max' { noi di in re "max() must be greater than min()" exit 98 } local y "`varlist'" qui { replace `y' = cond(`x'>=`min' & `x'<`min'+`s',`min',.) `ifuse' `inuse' local top = `min' + `s' while `top'<`max' { local bottom = `top' local top = `bottom' + `s' replace `y' = `bottom' if (`bottom'<=`x') & (`x'<`top') `anduse' `inuse' } if "`midpoin'"!="" { replace `y' = `y' + (`s'/2) } } end