I had posted earlier to ask for help with in recoding specified
values of a variable to missing, starting with .a, .b, ..., and
most importantly, keeping the original value labels. (This is a
generalization of the question that originally arose under this
thread heading.) Nick Cox generously cooked up a more robust
and efficient piece of code than what I had vaguely stabbed at,
which I repost below, with one small bug fixed (about which I
consulted with him.)
*! 1.0.0 NJC 29 Oct 2009
program rectomiss
// For a specified vaiable, recode a list of values to missing
// and reassign the missing values.
version 8.2
gettoken varname codes : 0
confirm numeric variable `varname'
numlist "`codes'", int
local lblname : value label `varname'
if "`lblname'" == "" {
di as err "`varname' does not have value labels attached"
exit 498
}
tokenize "`c(alpha)'"
local i = 1
qui foreach x of local codes {
replace `varname' = .``i'' if `varname' == `x'
local label : label `lblname' `x' // I removed parens
if "`label'" == "" local label "`x'"
label def `lblname' .``i'' "`label'", add
local ++i
}
end
// sample usage
// rectomiss gender 8 9
Sincerely,
=-=-=-=-=-=-=-=-=-=-=-=-=
Mike Lacy
Fort Collins CO USA
(970) 491-6721 office
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/