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

st: RE: Re: Tabulating "missing" values


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Re: Tabulating "missing" values
Date   Thu, 10 Apr 2003 18:46:27 +0100

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 
n.j.cox@durham.ac.uk 
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index