Statalist The Stata Listserver

[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: an ordinary two-way table

From   "Michael Blasnik" <>
To   <>
Subject   Re: st: an ordinary two-way table
Date   Fri, 11 May 2007 11:10:06 -0400

I just recalled that I had written a program with the same goal a while back and it will do what you ask:

program define tabvals
version 8.0
*! v 1.0 M Blasnik
syntax varlist( min=1 max=2) [if] [in], values(numlist) [MISSing] *
if "`missing'"!="" {
local novarlist "novarlist"
marksample touse , `novarlist'
local var: word 1 of `varlist'
qui levels `var' if `touse', local(hasvals) `missing'
local addvals: list values - hasvals
local tempobs: word count `addvals'
if `tempobs'>0 {
local origN=_N
qui expand `tempobs'+1 in l
tempvar real
qui gen byte `real'=(_n<=`origN')
qui replace `touse'=1 in -`tempobs'/l
forvalues i=1/`tempobs' {
local obsn=`origN'+`i'
local val: word `i' of `addvals'
qui replace `var'=`val' in `obsn'

noi tab `varlist' if `touse', subpop(`real') `missing' `options'
else {
noi tab `varlist' if `touse' , `missing' `options'

Hopefully there aren't any word wrap problems or you may need to fix this code up. It lets you specify a values numlist that applies to the first variable in the tabulate command. It allows you to specify a second variable but does not create new values for that one. Since it relies on Stata's tabulate command, it provides all of the options allowed with tabulate.

Michael Blasnik

----- Original Message ----- From: "Giovanni Vecchi" <>
To: <>
Sent: Friday, May 11, 2007 10:07 AM
Subject: RE: st: an ordinary two-way table

Chris and Nick, thanks for your suggestions. As you both say, the
problem with tabcount is that it does not allow row and column totals.

I'm always puzzled when I find Stata unable to offer easy answers to
easy questions (as the one raised in my posting). Faced with the
failure to produce a simple two-way table, I ask myself: is it me or
is it Stata? And my answers start looping ... "it's me", "it's Stata",
"it's me", "it's Stata",... time goes by, frustration/irritation start
off, etc...

A quick reaction to Nick's suggestion. In my opinion, there is an
issue on the opportunity cost of pursuing a Mata-based strategy.  My
guess is that it is high for most Stata users.

My point: ordinary two-way tables should be within the reach of
Mata-almost-illiterate users. Needless to say, I am an outstanding
member of this club.

A bottom line: hope StataCorp is sympathetic to my argument... :)


- Giovanni
*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index