[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: Keep value labels after -mvdecode-

From   "Nick Cox" <>
To   <>
Subject   st: RE: Keep value labels after -mvdecode-
Date   Thu, 5 Nov 2009 17:49:32 -0000

Thanks for the mention. More importantly: It is always good to see
concise, precise and incisive closures to threads. 

Anyone borrowing the code should prepend the line 

*! 2.0.0 Mike Lacy 5 Nov 2009 fixed NJC bug 

and attribute all credit, and contribute all further bug reports, to


-----Original Message-----
[] On Behalf Of Mike Lacy
Sent: 05 November 2009 17:23
Subject: st: Keep value labels after -mvdecode-

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
// sample usage
// rectomiss gender 8 9

Mike Lacy
Fort Collins CO USA
(970) 491-6721 office

*   For searches and help try:

*   For searches and help try:

© Copyright 1996–2023 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index