Bookmark and Share

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


[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   "Dimitriy V. Masterov" <[email protected]>
To   [email protected]
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 <[email protected]> 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/


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