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

st: Counting Unique Values by Year

From   "Jennifer S. Earl" <>
Subject   st: Counting Unique Values by Year
Date   Sun, 01 Jun 2003 17:34:36 -0700

I have a data set with cases spread out over a number of years. I have a numeric variable called CLMS. I want to create a new variable UNIQCLMS that equals the number of unique values that CLMS took on each year.

I have thought of some very long-winded ways to do this, such as creating a counter using a lag-comparison and then harvesting the last value of this counter, but it seems like it should be easier. In particular, Stata already calculates the number of unique values in lots of operations, including INSPECT (e.g., "by year: inspect clms" will produce the number of unique values for CLMS, unless that number exceeds 99, but it won't write that value out to another variable as far as I know), and the number of unique values should also equal the number of rows produced using "by year: tab clms".

So, I am hoping someone might be able to think of a quick and/or elegant way to get Stata to produce a new variable, UNIQCLMS that contains the number of unique values that CLMS takes on in each year. If I could dream up a new egen command, the format would be something like:

by year: egen uniqclm=unique(CLMS)

Any thoughts would be appreciated.



Jennifer Earl
Assistant Professor
Department of Sociology
University of California
Santa Barbara, CA 93106-9430

P: (805) 893-7471
F: (805) 893-3324

* For searches and help try:

© Copyright 1996–2019 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index