program define repshow, rclass
*!	Version 1.7 by Mead Over  14Dec21
*!	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) [required] & Listvars(varlist) [optional] 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 are passed to that command.

	version 17
	syntax anything(equalok) [if] [in] [, NOPromote ID(varname) Listvars(varlist) Verbose *]

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

	gettoken varname : anything , parse(= )

	local oldtype :  type `varname'
	tempvar oldversion

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

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

	//  Could further enhance by using -compare- here
	qui count if `oldversion' ~= `varname'
	local ndif = r(N)

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

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

		di _n in gr  "Comparison of -" in ye "`varname'" 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 "`varname'" in gr "- before the replace."
			di    in gr  "  The column -" in ye "New vrsn" in gr "- gives the values of -" in ye "`varname'" 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' `varname' `listvars' if `oldversion' ~= `varname' , /*
			*/ subvar `statopts' `options'
	}
	char define `varname'[varname] "`oldchar'"
	return local ndif `ndif'
	return local repvar `varname'


end  /* End of main program */

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 */
*	Ver. 1.4         adds the -nopromote- option which is available in STATA's -replace- command.
*	Ver. 1.5 10Mar09 fixes bug which would crash -repshow- if listvars are string variables
*	Ver. 1.6 17Jun21 returns the number of differences = the number of replacements as r(ndif)
*	Ver. 1.7 14Dec21 Thanks to advice from Stata's Chris Chen on using anything(equalok),
*		-repshow- now works with string variables