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

RE: st: n-way tabulation


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: n-way tabulation
Date   Thu, 3 Nov 2005 00:50:56 -0000

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. 

In your case 

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 
[email protected] 

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/



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