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

Re: st: SAS like command?

From   n j cox <>
Subject   Re: st: SAS like command?
Date   Thu, 18 Aug 2005 14:03:55 +0100

A comment on

bysort a b c: egen tabulation = sum(1)

I have used -egen, sum(1)- myself in examples, but it also
involves Stata interpreting about 100 lines, when one
will do the same job:

bysort a b c : gen tabcount = _N

On the original problem, I would suggest

egen group = group(a b c), label
tabstat n, by(group) stat(N sum)

for interactive use. For a program, given that
properly labelled output is essential for
the problem, I would probably use the same -egen-
call, a few -generate-s and a -tabdisp- for the display.


>>> James Muller

Here's a rough procedure:

Start with your dataset. Choose the variables you want to keep, suppose these are variables a, b and c
. bysort a b c: egen tabulation = sum(1)
. duplicates drop
Then you should have the below table as your new dataset.

Here's a simple example program that will simply output the data. If you're going to use this frequently, save it as tabNway.ado and put it in your adopath.

program define tabNway
keep `*'
by `*': egen tabcount = sum(1)
duplicates drop

In your case you would use input 'tabNway a b c'. I didn't use a tempvar because that gives a meaningless heading in the list. To get your other heading you can edit the program.

* For searches and help try:

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