Reputation: 455
I am trying to create many tables of cross-tabs in the style of tab
(twoway) or tabout
in Stata. However, I want to include response options for both variables, even if every cell for that variable in the twoway tabulation would be zero. For instance, if we alter the auto dataset slightly:
sysuse auto
tab rep78 foreign
* Altering data
replace foreign=. if foreign==0 & rep78==1
tab rep78 foreign
In the altered data, it is possible that there could be a car with rep78==1
, but there are no instances of that in the data among observations that are non-missing for the variable foreign
.
What I want is a table like:
Repair
Record Car type
1978 Domestic Foreign Total
1 0 0 0
2 8 0 8
3 27 3 30
4 9 9 18
5 2 9 11
Total 46 21 67
Nick Cox's very nice tabcount
works for this purpose but
and
tabout
. I often have to create batches of tables where a lot of cells in the tables will have zero observations due to missingness, and I often have many, many possible response options.Any ideas on a workaround that will allow me to still use tabout
or something similar?
Upvotes: 2
Views: 718
Reputation: 37208
As in my initial comments, there has to be some sense in which Stata is told what defines the rows and columns, or how to find that out. Nevertheless, this may help:
* ssc inst tabcount
sysuse auto, clear
tab rep78 foreign
levelsof rep78, local(rows)
levelsof foreign, local(cols)
replace foreign=. if foreign==0 & rep78==1
tabcount rep78 foreign, v1(`rows') v2(`cols') zero
Notes:
tabcount
must be installed before you do this. Use ssc inst tabcount
to do that. The command above is commented out as a signal that it is not needed after the first use.
You could write your own command or do-file based on looking-up values in the data before looking at the cross-combination desired.
The paper here may be helpful as a broader discussion.
Upvotes: 1