Count unique values in Stata

25,554

Solution 1

You can easily write a wrapper for codebook that uses Nick's distinct command from SSC to store the info you want as scalar(s).

In my experience, this wrapper approach has proven to be much more effective than asking the nice folks at StataCorp to change their command on an internet forum that they do not participate in.

Here's an example:

* (1) You can stick this into a file called mycodebook.ado in
* /ado/personal (use adopath to see exact location)
capture program drop mycodebook
program mycodebook, rclass
syntax [varlist] [if] [in][, *]
codebook `varlist' `if' `in', `options'
capture ssc install distinct
foreach var of varlist `varlist' {
    qui distinct `var' `if' `in'
    return scalar nv_`var' = r(ndistinct)
}
end

* (2) example with mycodebook
sysuse auto, clear
mycodebook price mpg rep78 if foreign==0, compact
return list

This last part will give you:

. mycodebook price mpg rep78 if foreign==0, compact

Variable   Obs Unique      Mean   Min    Max  Label
----------------------------------------------------------------------------------
price       52     52  6072.423  3291  15906  Price
mpg         52     17  19.82692    12     34  Mileage (mpg)
rep78       48      5  3.020833     1      5  Repair Record 1978
----------------------------------------------------------------------------------

. return list

scalars:
           r(nv_rep78) =  5
             r(nv_mpg) =  17
           r(nv_price) =  52

You can then do things like (or whatever it is you want to do with these):

gen x=r(nv_rep78)

Solution 2

A convenient alternative is provided by the "unique" package. Here's a quick example:

* Install the unique package
ssc inst unique

* Load toy dataset
sysuse auto, clear

* Get a quick report of unique (and total) values for a variable
unique mpg

* The result will be available as r(unique)
return list
Share:
25,554
emeryville
Author by

emeryville

If I answer your question, I always upvote your question.

Updated on May 01, 2021

Comments

  • emeryville
    emeryville about 3 years

    codebook is a great command in Stata. It describes data contents but also simply identifies unique values

    sysuse auto, clear
    codebook mpg, compact
    

    Number of unique values of mpg is 21. Looking at the help of the command, it does not seem possible to store this value. Am I wrong?

    I am aware of other ways to compute the number of unique values in Stata, but it would be so convenient to add this feature to the codebook command.

  • emeryville
    emeryville about 8 years
    I am afraid I am not wrong! Thanks but I am interested in storing this value after codebook and it seems not possible. See Nick's answer.
  • ℕʘʘḆḽḘ
    ℕʘʘḆḽḘ about 8 years
    you can basically reproduce each of codebook s output manually. and nvals gives you thr unique values
  • Nick Cox
    Nick Cox about 8 years
    The latest version of distinct (authors Gary Longton and myself) is to be downloaded from the Stata Journal website. search distinct, sj in Stata to get a link for installation.
  • David Ansermot
    David Ansermot about 8 years
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
  • Nick Cox
    Nick Cox about 8 years
    @David 'mArm' Ansermot It is an answer. Generating a new variable with the number of distinct values is an alternative to codebook. This is concise, but not cryptic if you read the documentation for the package mentioned. As the original author of both code solutions mentioned so far in this thread, I'd emphasise a different answer personally, but Noobie is answering here.
  • David Ansermot
    David Ansermot about 8 years
    @NickCox While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. meta.stackoverflow.com/questions/300837/…
  • Nick Cox
    Nick Cox about 8 years
    I don't disagree with that, but the assertion made "does not provide an answer" is too harsh and (I have to suggest) based on seeing a brief answer rather than knowing a lot about this language. I don't see that people answering a question are obliged to remind others of basics about a language, in this case to read the help page associated with a command or package.
  • Nick Cox
    Nick Cox about 2 years
    unique clearly can be useful, but this doesn't address the question, which is about extending or modifying codebook.