Michael Blasnik
>
> 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
A hour ago I hacked privately at Shannon's -tabrange-,
called it -tabvalues- and asked him what he thought,
but somehow or other we'll perhaps be able to get these efforts to
coalesce.
Avoiding the -preserve- by checking whether
all the values are present is a good move.
Nick
[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/