Statalist


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

st: Keep value labels after -mvdecode-


From   Mike Lacy <Michael.Lacy@colostate.edu>
To   statalist@hsphsun2.harvard.edu
Subject   st: Keep value labels after -mvdecode-
Date   Thu, 05 Nov 2009 10:23:23 -0700


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/



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