Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • mvdecode equivalent for string values? or, how do I replace all "na" values in my dataset?

    I'm looking for a function that will do with "NA" and "UA" throughout my dataset what mvdecode does for "-999"

    If mvdecode will change numeric values to missing values, what function will do the same for string values?

    Thanks very much in advance!

  • #2
    assuming that the variable is a string variable (rather than a numeric variable with labels) and that "NA" and "UA" appear with nothing else (e.g., no "NA2", etc.), the following should work:

    replace var="" if inlist(var,"NA","UA")

    one potential problem exists if there are spaces before or after then "NA" or "UA" and you might first want to use the "trim" function to get rid of any such spaces (see -h trim-)

    Comment


    • #3
      mvdecode is not a function in Stata terms; it's a command. But terminology aside, you need a command to do what you ask, and the command is just replace. The trick is to call it in a loop.

      Code:
      ds, has(type string) 
      
      quietly foreach v in `r(varlist)' { 
          replace `v' = "" if inlist(`v', "NA", "UA") 
      }
      That said, it is hard to see that this is a good idea. The "NA" and "UA" mean something. Replacing them with string missing "" removes information from the dataset.

      It is more likely that if you have a series of answers in string variables that they should be encoded and the "NA" and "UA" (whatever they mean) should be become missing numeric values with value labels attached.

      See also multencode (SSC).

      Comment


      • #4
        Thanks, gentleman! Much appreciated.

        Comment

        Working...
        X