# Re: st: number of unique values within an observation

 From "Svend Juul" To Subject Re: st: number of unique values within an observation Date Fri, 30 Sep 2005 22:30:33 +0200

```Scott Imberman wrote:

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
-------------------

In Stata terminology the information for the four years for one student
is not organized in one, but four observations (long format). A solution
follows here (but I guess there are simpler ways):

------------------------------------------------------------------------
clear
input student year school
17 1995 3
17 1996 2
17 1997 3
17 1998 3
18 1995 1
18 1996 1
18 1997 1
18 1998 1
19 1995 1
19 1996 2
19 1997 3
19 1998 4
end

gen x=1
sort student school
list
replace x=x[_n-1]+1 if student==student[_n-1] & school != school[_n-1]
replace x=x[_n-1] if student==student[_n-1] & school == school[_n-1]
by student: gen nschools=x[_N]

list
+----------------------------------------+
| student   year   school   x   nschools |
|----------------------------------------|
1. |      17   1996        2   1          2 |
2. |      17   1997        3   2          2 |
3. |      17   1995        3   2          2 |
4. |      17   1998        3   2          2 |
5. |      18   1997        1   1          1 |
|----------------------------------------|
6. |      18   1996        1   1          1 |
7. |      18   1995        1   1          1 |
8. |      18   1998        1   1          1 |
9. |      19   1995        1   1          4 |
10. |      19   1996        2   2          4 |
|----------------------------------------|
11. |      19   1997        3   3          4 |
12. |      19   1998        4   4          4 |
+----------------------------------------+

------------------------------------------------------------------------

Hope this helps,
Svend
________________________________________________________

Svend Juul
Institut for Folkesundhed, Afdeling for Epidemiologi
(Institute of Public Health, Department of Epidemiology)
Vennelyst Boulevard 6
DK-8000 Aarhus C,  Denmark
Phone, work:  +45 8942 6090
Phone, home:  +45 8693 7796
Fax:          +45 8613 1580
E-mail:       sj@soci.au.dk
_________________________________________________________

*
*   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/
```