program define repshow
*!	Version 1.5 by Mead Over   10Mar09
*!	syntax varname =exp [if] [in] [, NOPromote ID(varname) Listvars(other vars to be listed) Verbose * ]
*!	        (* = Other options for STATA's -list- command )
*	Show the changes made by the -replace- command. Basic syntax is exactly like -replace-.
*	Options ID(varname) & Listvars(varlist) display the values of other variables for the same
*	observations that are changed by the -replace- command.  These options are for information only.
*	In addition any options which typically apply to the -list- commmand can be added.
*	First developed under the name "repwhich.ado"

*	Version 1.4 adds the -nopromote- option which is available in STATA's -replace- command.
*	Version 1.5 fixes bug which would crash -repshow- if listvars are string variables

	version 9.2
	syntax varname =exp [if] [in] [, NOPromote ID(varname) Listvars(varlist) Verbose *]

	if "`verbose'"=="" {
		local shhh quietly
	}

	local oldtype :  type `varlist'
	tempvar oldversion

	qui gen `oldtype' `oldversion' = `varlist'
		char define `oldversion'[varname] "Old vrsn"

	replace `varlist' `exp' `if' `in' , `nopromote'
		local oldchar :  char `varlist'[varname]
		char define `varlist'[varname] "New vrsn"

	qui count if `oldversion' ~= `varlist'
	local ndif = r(N)

	if `ndif' == 0 {
		`shhh' di in gr "No observations have been changed."
	}
	else {

		if `ndif'>1 {
            statvars , ovrsn(`oldversion') vname(`varlist') listvars(`listvars')
            local statopts "mean(`r(statvars)') N(`r(statvars)')"
		}

		di _n in gr  "Comparison of -" in ye "`varlist'" in gr "- before and after execution of -replace-"

		if "`shhh'"=="" {

			if "`id'"~="" {
				di    in gr  "  The column -" in ye "`id'" in gr "- identifies the observations"
			}
			di    in gr  "  The column -" in ye "Old vrsn" in gr "- gives the values of -" in ye "`varlist'" in gr "- before the replace."
			di    in gr  "  The column -" in ye "New vrsn" in gr "- gives the values of -" in ye "`varlist'" in gr "- after the replace."
			if "`listvars'"~="" {
				di    in gr  "  The column(s) " in ye "`listvars'" in gr " are for information only."
			}

		}

		list `id' `oldversion' `varlist' `listvars' if `oldversion' ~= `varlist' , /*
			*/ subvar `statopts' `options'
	}
	char define `varlist'[varname] "`oldchar'"

end
program define statvars, rclass
    syntax [, vname(varname) ovrsn(varname) listvars(varlist) ]

    foreach X of varlist `vname' `ovrsn' `listvars' {
        capture confirm numeric variable `X'
        if _rc == 0 {
            local statvars `statvars' `X'
        }
    }
    return local statvars `statvars'
end  /* End of program statvars */