Count missing observations

With one variable, that's easy enough: count if missing(variable-name). If you have several variables, you can put them in a foreach loop. But if you have to do this for arbitrary lists of variables in several files, it may be interesting to package that foreach loop inside a quick command that might handle special display instructions as well.

Here is one suggestion:

// countIfMissing: display the total count of observations, then
// any counts of missing observations for each variable in a list.
capture prog drop countIfMissing
program countIfMissing

version 11
syntax varlist

quietly count
local count=r(N)

// now make things align nicely
local sum=`count'
local tens=1
while `sum'/10>1 {
   local sum=`sum'/10
   local tens=`tens'+1
}
local width=`tens'+int(`tens'/3)
local varct: list sizeof varlist

di ""
di "Observations:"
di %`width'.0fc `count'
di ""
di "Missing:"
foreach varble in `varlist' {
   qui count if missing(`varble')
   local ct=r(N)
   local pct: di %4.2fc 100*`ct'/`count'
   if `pct'>0 {
      di %`width'.0fc `ct' " `varble' (`pct'%)"
   }
   else {
      local varct=`varct'-1
   }
}
if `varct'==0 {
   local offset=`width'+2
   di _column(`offset') "none of `varlist'"
}
di ""

end

For an example of usage, you can try this:

sysuse auto
local myvars "make price foreign"
countIfMissing `myvars'
countIfMissing m*       // (1)
countIfMissing _all     // (2)

As you can see in (1) and (2), the usual varlist conveniences apply here.

3 Responses to “Count missing observations”

  1. Martin Weiss writes:

    One may add that -inspect- and -codebook, mv- can do part of the job above and are shipped with official Stata. Re your example, users should omit the "//" comments when running this code interactively, as "//" only works in do/ado-files.

  2. Nick Cox writes:

    A program -nmissing- is also available to do essentially this. In Stata,

    . search nmissing

    Keyword search

    Keywords: nmissing
    Search: (1) Official help files, FAQs, Examples, SJs, and STBs

    Search of official help files, FAQs, Examples, SJs, and STBs

    FAQ . . . . . . Can I quickly see how many missing values a variable has?
    . . . . . . . . . . . . . . . . . . UCLA Academic Technology Services
    7/08 http://www.ats.ucla.edu/stat/stata/faq/nmissing.htm

    Example . . . . . . . . . . . . . . . . . . . . Useful non-UCLA Stata programs
    . . . . . . . . . . . . . . . . . . UCLA Academic Technology Services
    7/08 http://www.ats.ucla.edu/stat/ado/world/

    SJ-5-4 dm67_3 . . . . . . . . . . Software update for nmissing and npresent
    (help nmissing if installed) . . . . . . . . . . . . . . . N. J. Cox
    Q4/05 SJ 5(4):607
    now produces saved results

    SJ-3-4 sg67_2 . . . . . . . . . . Software update for nmissing and npresent
    (help nmissing, npresent if installed) . . . . . . . . . . N. J. Cox
    Q4/03 SJ 3(4):449
    updated to include support for by, options for checking
    string values that contain spaces or periods, documentation
    of extended missing values .a to .z, and improved output

    STB-60 dm67.1 . . . . Enhancements to numbers of missing and present values
    (help nmissing if installed) . . . . . . . . . . . . . . . N. J. Cox
    3/01 pp.2--3; STB Reprints Vol 10, pp.7--9
    updated with option for reporting on observations

    STB-49 dm67 . . . . . . . . . . . . . Numbers of missing and present values
    (help nmissing if installed) . . . . . . . . . . . . . . . N. J. Cox
    5/99 pp.7--8; STB Reprints Vol 9, pp.26--27
    commands to list the numbers of missing values and nonmissing
    values in each variable in varlist

  3. Michael P. Manti writes:

    I'm a big fan of -inspect-, which provides useful diagnostics, counts of missing values among them.

Leave a Reply