# Re: Re: st: create a contingency table

 From "Nick Cox" To Subject Re: Re: st: create a contingency table Date Tue, 4 Nov 2003 23:40:52 -0000

```Jean-Benoit Hardouin

> Thank you Nick, but my question is :
> How to obtain a data table and not to display the results ?
> How, with a simple syntax, create the variables of the second table
?
> Jean-Benoit Hardouin
>
> On Tue, 4 Nov 2003 23:05:33 -0000, Nick Cox <n.j.cox@durham.ac.uk>
wrote:
>
> > Jean-Benoit Hardouin
> >
> >> I search a method to transform a data as :
> >> Item1 Item2 Item3 Item4 0 0 1 2 1 3 0 1
> >> 3 0 2 1
> >
> >> (table of the responses of individuals to four items) in
> >> Item1 Item2 Item3 Item4 Count
> >> 0 0 0 0 125
> >> 0 0 0 1 88
> >> 0 0 0 2 72
> >> 0 0 0 3 52
> >> 0 0 1 0 28
> >> 0 0 1 1 65
> >> 0 0 1 2 42
> >> ...
> >
> >> where 125 is the number of individuals who have respond 0 to each
> > item, 88
> >> is the number of individuals who have respond 0 to the first
three
> > item and
> >> 1 to the last item, etc.
> >
> >> I don't find how to obtain this kind of table with "reshape" or
> > "egen" (for
> >> example).
> >
> >> I have idea to program that with four "forvalues..." but perhaps
it
> > exist a
> >> specific command.
> >
> > For a first-principles solution:
> >
> > bysort Item? : gen Count = _N
> > by Item? : gen tag = _n == 1
> > list Item? Count if tag
> >
> > For a canned solution, with extra bells and whistles:
> >
> > ssc inst groups
> > groups Item? , show(f) showhead(Count)
> > whelp groups
> >

bysort Item? : gen Count = _N
by Item? : gen tag = _n == 1
list Item? Count if tag

If you want just to keep the
table as new dataset:

keep if tag
keep Item? Count

_OR_

contract Item?

See [R] contract.

I guess I would have suggested that earlier
if I had sensed interest in dataset as result,
rather than table as result.

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/
```