Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Number of cases within a variable

    Dear Statalisters,
    I was wondering if anyone can help me with a more efficient (programming wise) method to determine the number of different values that a variable has.
    For example, lets assume the following data:
    set obs 1000
    gen dm=_n
    drop if runiform()<.3
    expand ceil(runiform()*40)

    The way i would usually determine the number of cases is
    codebook dm
    and if i want to store it i would use the following code:

    egen _k=group(dm)
    sum _k
    and the number of unique values will be stored in r(max).

    I would appreiate if anyone knows of a faster method.
    Thanks
    Fernando Rios

  • #2
    Does a simple tabulate dm give you what you want? You might wish to suppress the output. Look at the stored results, especially r(r).


    Best
    Daniel

    Comment


    • #3
      Thanks Daniel,
      I didnt know of that. Although I think tabulate has some upper limit number of cases, which might be very very large for the problem Im looking at.
      Fernando

      Comment


      • #4
        built in to Stata are -codebook- and -inspect-, each of which will give you what I think you are requesting; there are also two user-written routines (use -search- to find them): -unique- and -distinct-

        Comment


        • #5
          in another posting, Nick Cox mentioned "egenmore" from SSC. In perusing it to see what it is capable of, I ran across "nvals." So if you get egenmore installed, it should be as simple as:

          egen nvals_x1=nvals(x1)

          Comment


          • #6
            To expand on the posting by Rich Goldstein:

            For a review of this question introducing a distinct command see

            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

            The review is accessible to all at http://www.stata-journal.com/sjpdf.h...iclenum=dm0042

            The program was updated in Stata Journal 12(2).
            Last edited by Nick Cox; 27 Aug 2014, 18:55.

            Comment


            • #7
              Thank you very much Rich, Ben, and Nick.
              Best

              Comment

              Working...
              X