# Re: st: n-way tabulation

 From Lawrence Hanser To statalist@hsphsun2.harvard.edu Subject Re: st: n-way tabulation Date Wed, 2 Nov 2005 17:08:56 -0800

```Brilliant!  Worked perfectly.  Thanks.

BTW, I have a new colleague who is new to Stata, and she is already
impressed both with the ease of Stata and the brain trust of online
colleagues.

On 11/2/05, Nick Cox <n.j.cox@durham.ac.uk> wrote:
> To -*contract- and then to -merge- to get back to
> the original data, plus some, is a double cross-continental
> journey when you could stay at home. You might enjoy the
> travel, but there's a simpler way.
>
> You need to master -by:-, as someone I believe suggested
> in the original thread. All these commands
> such as -contract-, -xcontract- and -groups-
> are just variation exercises on the use of -by:-.
>
> There is a somewhat long-winded tutorial at
>
> How to move step by: step.
> Stata Journal 2(1): 86-102 (2002)
>
> that explains the use of -by:- to tackle
> a variety of problems with group structure, ranging from
> simple calculations for each of several groups to more
> advanced manipulations that use the built-in _n and _N.
>
> But the key ideas are all in the manual, just scattered
> around a few short sections.
>
>
> bysort religion race gender : gen long _freq = _N
>
> would solve your example in a single bound.
>
> Incidentally, -groups- as recommended by Svend Juul
> gets you a table without a restructure, although it
> does not -generate- anything, even optionally.
>
> Nick
> n.j.cox@durham.ac.uk
>
> Lawrence Hanser
>
> > Thanks, xcontract worked perfectly.
> >
> > Now I have an additional question.  My dataset of cell frequencies
> > (created by xcontract) has something like 140,000 observations.  My
> > original dataset has 648,000 observations.  I need to attach the cell
> > frequencies back to the original dataset.  I thought this would be
> > something like  -merge, but it wants unique observations...
> >
> > Imagine something like this in the original data:
> >
> > religion  race    gender
> >   1          1           1
> >   1          1           1
> >   1          1           2
> >   1          2           1
> >   1          2           1
> >
> > the data created by -xcontract are:
> >
> > religion  race    gender  _freq
> >   1          1           1         2
> >   1          1           2         1
> >   1          2           1         2
> >
> > I want my original dataset to be:
> >
> > religion  race    gender  _freq
> >   1          1           1        2
> >   1          1           1        2
> >   1          1           2        1
> >   1          2           1        2
> >   1          2           1        2
> >
> > I tried -merge ....., uniqusing
> > where my frequency dataset was the using dataset, but it was still
> > looking for unique values of the -merge varlist in the master dataset.
>
> *
> *   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/
>

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