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

st: Re: check whether a variable has any missing values

From   "Martin Weiss" <>
To   <>
Subject   st: Re: check whether a variable has any missing values
Date   Sat, 28 Mar 2009 21:12:30 +0100


In addition to other advice, Jacob might like -findit mdesc-. After installation, run

sysuse auto, clear
mdesc  rep78
ret li

to see that the missing value count is conveniently returned in r(miss)...

----- Original Message ----- From: "Jacob Wegelin" <>
To: <>
Sent: Saturday, March 28, 2009 7:57 PM
Subject: st: check whether a variable has any missing values

The following program checks whether a variable contains any missing
values. This seems an awkward way to do it. Does a more elegant
approach exist? I haven't found an extended macro function that takes
care of this, and the function -missing()- does not do the trick.

clear all
capture program drop checkIfAnyMissing
program define checkIfAnyMissing, rclass
  return local variable="`1'"
  qui tabulate `1'
  local howManyObs=`r(N)'
  qui tabulate `1', missing
  local howManyRowInclMissing=`r(N)'
  if (`howManyObs'== `howManyRowInclMissing') {
     return local answer="`1' has no missing values"
     return scalar missing=0
  else {
     return local answer="`1' has at least one missing value"
     return scalar missing=1

input bad good str8 strbad str8 strgood
0 0 0 0
. 1 "" 1


di missing(bad)

di missing(good)

checkIfAnyMissing bad
return list

checkIfAnyMissing good
return list

checkIfAnyMissing strbad
return list

checkIfAnyMissing strgood
return list

Thanks for any tips

Jacob Wegelin
Assistant Professor
Department of Biostatistics
Virginia Commonwealth University
730 East Broad Street Room 3006
P. O. Box 980032
Richmond VA 23298-0032
*   For searches and help try:

*   For searches and help try:

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