capture program drop demean program define demean quietly sum `1' replace `1'=`1'-r(mean) end clear use /software/stata/auto demean mpg sum mpg capture program drop demean program define demean foreach var of local 0 { quietly sum `var' replace `var'=`var'-r(mean) } end clear use /software/stata/auto demean mpg price rep78 sum mpg price rep78 clear use /software/stata/auto capture noisily demean make mpg price rep78 /* capture noisily tells the command to generate an error message, but not crash and stop the rest of the do file. */ sum make mpg price rep78 capture program drop demean program define demean foreach var of local 0 { capture confirm numeric variable `var' if _rc==0 { quietly sum `var' replace `var'=`var'-r(mean) } else di "`var' is not a numeric variable and cannot be demeaned." } end clear use /software/stata/auto demean make mpg price rep78 sum make mpg price rep78