Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: number of distinct values for a variable in a group using -collapse-

From   Nick Cox <>
Subject   Re: st: number of distinct values for a variable in a group using -collapse-
Date   Fri, 24 Feb 2012 23:57:59 +0000

For this to work, you would have to install -egenmore- (SSC) first.

There is a detailed review of this question in

SJ-8-4  dm0042  . . . . . . . . . . . .  Speaking Stata: Distinct observations
        (help distinct if installed)  . . . . . .  N. J. Cox and G. M. Longton
        Q4/08   SJ 8(4):557--568
        shows how to answer questions about distinct observations
        from first principles; provides a convenience command

and -search distinct- would have pointed you to this directly.


On Fri, Feb 24, 2012 at 7:00 PM, Dimitriy V. Masterov
<> wrote:

> Collapse does not have this useful option, but you can do this using
> egenmore and nvals:
> sysuse auto, clear
> tab rep78 foreign
> bys foreign: egen n=nvals(rep78)
> collapse (max) n (mean) price, by(foreign )
> list
> To learn more about this, type -h egenmore-.

On Fri, Feb 24, 2012 at 1:50 PM, Lloyd Dumont <> wrote:

>> Is there an extension to -collapse- that counts the number of distinct observations for a variable?
>> For example, say I have a dataset of individuals, each of whom is assigned to a group.  Each individual also has a job title, which is stored as a categorical.
>> So, let's say I wanted to -collapse- this dataset into a dataset of groups.  It would be easy to count the number of people in each group.  But, what if I also wanted to know the number of distinct job titles in each group?  For example, in some groups, all of the individuals would hold the same job title.  In other groups, each of the, say, 5 people in the group might have a different job title.

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index