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

Re: st: number of unique values within an observation


From   Raphael Fraser <raphael.fraser@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: number of unique values within an observation
Date   Fri, 30 Sep 2005 16:03:11 -0500

Scott,

Yes there is! A user written command called -unique-. Just type findit
unique and install.

Description

The command unique without a by reports the number of unique values for
the varlist. With a by it does the same, but also creates a new
variable (_Unique by default but can be named in the gen option). This new
variable contains the number of unique values of the varlist for each level
of the by variable. The new variable is coded missing except for the first
record in each group defined by the levels of the by variable.


On 9/30/05, Eric G. Wruck <ewruck@econalytics.com> wrote:
> Hi Scott:
>
> Here is a way to do it:
>
> . egen unique = tag(id)
>
> . egen no_unique = total(unique)
>
> . l
>
>      +-------------------------------+
>      | year   id   unique   no_uni~e |
>      |-------------------------------|
>   1. | 1995    3        1          2 |
>   2. | 1996    2        1          2 |
>   3. | 1997    3        0          2 |
>   4. | 1998    3        0          2 |
>      +-------------------------------+
>
> .
>
> Of course, you may want to do this by(student).
>
> Eric
>
>
>
> >Dear Statalist,
> >
> >I was wondering if there is s a simple way to count the number of
> >unique values in a variable list for each observation.  For example, i
> >am looking at the id numbers for colleges that a set of student attends.
> > A student may list having attended a college in multiple years like
> >this:
> >
> >year     id
> >
> >1995     3
> >1996     2
> >1997     3
> >1998     3
> >
> >I want to know how many unique schools the student attended (in this
> >example the answer is 2).  Thanks for your help.
> >
> >Sincerely,
> >
> >Scott Imberman
> >
> >-------------------------------------------------------------------
> >Scott Andrew Imberman
> >
> >Department of Economics
> >3105 Tydings Hall, University of Maryland
> >College Park, MD 20742
> >Voice: (301) 405-4534
> >Fax: (301) 405-3542
> >
> >imberman@econ.umd.edu
> >
> >*
> >*   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/
>
>
> --
>
> ===================================================
>
>        Eric G. Wruck
>        Econalytics
>        2535 Sherwood Road
>        Columbus, OH  43209
>
>        ph:      614.231.5034
>        cell:    614.330.8846
>        eFax:    614.573.6639
>        eMail:   ewruck@econalytics.com
>        website: http://www.econalytics.com
>
> ====================================================
> *
> *   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/
>


--
=============================
Raphael A. Fraser, MSc
Assistant Lecturer in Biostatistics
Sickle Cell Unit
Tropical Medicine Research Institute
Faculty of Medical Sciences
University of the West Indies
Kingston
Jamaica

Tel:  (876) 927-2471; 410-4699
Fax: (876) 927-2984
e-mail: raphael.fraser@uwimona.edu.jm

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index