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

st: Keep value labels after -mvdecode-

From   Mike Lacy <[email protected]>
To   [email protected]
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
// sample usage
// rectomiss gender 8 9

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

*   For searches and help try:

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