Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down on April 23, and its replacement, **statalist.org** is already up and running.

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

From |
"Dimitriy V. Masterov" <dvmaster@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

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

Date |
Fri, 24 Feb 2012 14:00:33 -0500 |

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-. DVM On Fri, Feb 24, 2012 at 1:50 PM, Lloyd Dumont <lloyddumont@yahoo.com> wrote: > Hello, everyone. > > Here is a simple one. I'll pose the question, and then I'll give an example, in case that helps. > > 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. > > Thank you for your help! Lloyd > > * > * For searches and help try: > * http://www.stata.com/help.cgi?search > * http://www.stata.com/support/statalist/faq > * http://www.ats.ucla.edu/stat/stata/ * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

**Follow-Ups**:**Re: st: number of distinct values for a variable in a group using -collapse-***From:*Nick Cox <njcoxstata@gmail.com>

**References**:**st: number of distinct values for a variable in a group using -collapse-***From:*Lloyd Dumont <lloyddumont@yahoo.com>

- Prev by Date:
**st: Program to simulate AR(1) time series and return autocorrelations** - Next by Date:
**Re: st: Problem while running a simple OLS regression** - Previous by thread:
**st: number of distinct values for a variable in a group using -collapse-** - Next by thread:
**Re: st: number of distinct values for a variable in a group using -collapse-** - Index(es):