Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: Tabulating "missing" values


From   "Michael Blasnik" <[email protected]>
To   <[email protected]>
Subject   st: Re: Tabulating "missing" values
Date   Thu, 10 Apr 2003 13:40:26 -0400

J. Michael Oakes asked about getting tab to show 0 counts for values that
could exist (but don't) when tabulating a variable.  Shannon Driver of Stata
provided one solution that bypasses the tab command entirely and instead
manipulates and collapses the dataset.

I have taken a somewhat different approach -- add some phony observations
showing the missing values of interest and then use the subpop option of tab
to exclude them.  Then Stata's built-in tab command can do the work for you.
This program could (and should) be expanded to take other tab command
options (matcell, etc.) I have lifted a name suggested by Nick Cox and
called it tabvalues.  Here it is (hopefully without wrapping problems):

program define tabvalues
version 8.0
syntax varname [if] [in], values(numlist) [MISSing]
if "`missing'"!="" {
 local novarlist "novarlist"
}
marksample touse , `novarlist'
qui levels `varlist' if `touse', local(hasvals) `missing'
local addvals: list values - hasvals
local tempobs: word count `addvals'
if `tempobs'>0 {
 preserve
 local origN=_N
 local origNp1=`origN'+1
 qui expand `tempobs'+1 in l
 tempvar real
 qui gen byte `real'=(_n<=`origN')
 qui replace `touse'=1 in `origNp1'/l
 forvalues i=1/`tempobs' {
  local obsn=`origN'+`i'
  local val: word `i' of `addvals'
  qui replace `varlist'=`val' in `obsn'
 }
 noi tab `varlist' if `touse', subpop(`real') `missing'
 restore
}
else {
 noi tab `varlist' if `touse' , `missing'
}
end

Michael Blasnik
[email protected]


*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   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