Statalist


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

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


From   David Airey <[email protected]>
To   [email protected]
Subject   Re: st: check whether a variable has any missing values
Date   Sat, 28 Mar 2009 15:07:19 -0500

.

assert works directly with missing() or !missing() ...

sysuse auto
assert !missing(mpg)
replace mpg = . in 1
assert missing(mpg)
assert !missing(mpg)


On Mar 28, 2009, at 2:50 PM, Jacob Wegelin wrote:

Sorry ... I should have thought before sending that last question.

qui count if missing(bad)

assert r(N)==0

Jacob Wegelin

On Sat, Mar 28, 2009 at 3:37 PM, Jacob Wegelin <[email protected] > wrote:
Dear Eva,

Thanks. How then does one turn the output of -count if missing(bad)-
into a value that one can use in an -assert- statement?

. count if missing(bad)
   1

. count if missing(good)
   0

. local howManyMissing=count if missing(bad)
count not found
r(111);

. local junk: count if missing(bad)
count not allowed
r(101);

. scalar junk=count if missing(bad)
count not found
r(111);

Jake

On Sat, Mar 28, 2009 at 3:12 PM, Eva Poen <[email protected]> wrote:
Jacob,

it's a lot easier than you think. I believe you are confused about the
missing() function.

di missing(bad)

instructs Stata to display whatever missing(bad) returns, which will
be zero if missing(bad) evaluates to fale, and one if it evaluates to true. Because bad is a variable, missing(bad) is actually interpreted
by Stata as missing(bad[1]), i.e. the value of the first observation
is substituted for the variable.

What you want is simply

count if missing(bad)

Hope this helps,
Eva


2009/3/28 Jacob Wegelin <[email protected]>:
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
     }
end

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

list

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:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



*
*   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/

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index