Statalist The Stata Listserver

[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: using information in value labels

From   n j cox <>
Subject   Re: st: using information in value labels
Date   Tue, 13 Feb 2007 13:48:47 +0000

I would not -recode- the original
variables. The scope for a large or even small mess
is such that it would be safer to do
this on copies of the original variables.
Or equivalently, keep your original
dataset file(s) very safe.

This is a sketch. Code not tested.

program fixmissing
	version 8
	syntax varlist
	qui ds `varlist', has(vallabel)
	local varlist "`r(varlist)'"

	foreach v of local varlist {
		qui levelsof `v', local(levels)
		foreach l of local levels {
			local label : label (`v') `l'
			if index(`"`label'"', "Missing") {
				recode `v' = .
			else if index(`"`label'"', "missing") {
				 recode `v'  = .
			else if index(`"`label'", "did not respond") {
				recode `v' = .a
			// more lines likely

Alexander Nervedi

I have a complex survey dataset which has been helpfully cleaned - each
variable has its missing values coded with 9/99/999/999 or 98/998/9998 etc.
and things like Missing, Did not respond, refused to answer etc. are all
coded in as value labels.

I would like to treat all of these as missing and want to run a loop as

local allvars x1 ... x100

foreach var of local allvars{

   label list `var'
  *pseudo code follows
   scan label to see which levels include - "Missing", "missing", "did not
respond", etc
  for each level `i' {
      recode `var' (`i' = .)


I  was wondering if this is something that could be reasonably be

*   For searches and help try:

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