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.