Thanks to José Maria for his publicity.
The program -distinct- on SSC is in fact by Gary Longton and myself. Also, the version on SSC is superseded by that from the SJ, as -njc_stuff- from SSC does indicate. The earlier version remains available for any people still on Stata 7.
This problem has arisen many times and various broadly similar programs have been written, as other answers in this thread indicated.
. search distinct
gives various materials. The last-mentioned paper is (I believe) the fullest discussion of the problem. Among other things, it gives reasons why "unique" is _not_ the best word to use here!
Keyword search
Keywords: distinct
Search: (1) Official help files, FAQs, Examples, SJs, and STBs
Search of official help files, FAQs, Examples, SJs, and STBs
[P] levelsof . . . . . . . . . . . . . . . . . . . . . Levels of variable
(help levelsof)
FAQ . . . . . . . . . . . . . . Calculating the number of distinct values
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
9/06 How do I calculate the number of distinct
values seen so far?
http://www.stata.com/support/faqs/data/distinctvalues.html
FAQ . . . . . . . . . Counting distinct strings across a set of variables
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
7/04 How do I count the number of distinct strings
across a set of variables?
http://www.stata.com/support/faqs/data/distinctstrings.html
FAQ . . . . . . . . . . . . . . . . . . . Number of distinct observations
. . . . . . . . . . . . . . . . . . . . . . . N. J. Cox and G. Longton
4/02 How do I compute the number of distinct observations?
http://www.stata.com/support/faqs/data/distinct.html
SJ-9-1 pr0046 . . . . . . . . . . . . . . . . . . . Speaking Stata: Rowwise
(help rowsort, rowranks if installed) . . . . . . . . . . . N. J. Cox
Q1/09 SJ 9(1):137--157
shows how to exploit functions, egen functions, and Mata
for working rowwise; rowsort and rowranks are introduced
SJ-8-4 dm0042 . . . . . . . . . . . . Speaking Stata: Distinct observations
(help distinct if installed) . . . . . . N. J. Cox and G. M. Longton
Q4/08 SJ 8(4):557--568
shows how to answer questions about distinct observations
from first principles; provides a convenience command
On the other hand, I've never used SAS and can't vouch for identical aims here.
Nick
[email protected]
José Maria (28 December 2009)
may be -distinct-, from SSC, writen by Nick Cox?
From: "Weichle, Thomas" <[email protected]>
> Is there a command that will list the number of unique individuals in my
> dataset? I'm familiar with the duplicates and dups commands, but these
> aren't showing me the number of unique individuals.
> It should be equivalent to the following SAS command which will identify
> the number of unique observations by study_id:
>
> PROC SORT NODUPKEY DATA=test;
> BY study_id;
> RUN;
*
* 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/