Announcement

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

  • list distinct values

    I'd like to look at the joint distribution of 3 or more discrete variables a,b,c,...
    I imagine a command called "list a b c, distinct" that would give output like this

    a b c count
    0 1 1 93
    1 1 1 95
    etc

    where "count" is the number of rows with the given values of a,b,c.

    In Unix, I can do this with "uniq -c". In SAS I can do it with PROC SQL, or with the LIST option in PROC FREQ tables.

    How can I do it in Stata?


  • #2
    either collapse or contract should do what you want

    Comment


    • #3
      Thank you. Those commands change the data. Is there any way to produce the desired output without changing the data?

      Comment


      • #4
        yes, but it is rather more complicated:
        Code:
        qui count if a==0 & b==1 & c==1
        local cat1=r(N)
        qui count if a==1 & b==1 & c==1
        local cat2=r(N)
        etc.
        you could, to make it easier, put it in a loop

        then just list remembering to list the local also

        Comment


        • #5
          If you don't mind sorting the data, something like this might do what you need.
          Code:
          by a b c, sort: generate count = _N
          egen tolist = tag(a b c), missing
          list a b c count if tolist
          drop count tolist
          Caveat: I haven't tested this, and I'm uncertain how well it will work if any of the variables are missing.

          Comment


          • #6
            groups has been available to do this since 2003. The only real write-up is, I regret, now incomplete.

            SJ-3-4 pr0011 . . . . . . . . Speaking Stata: Problems with tables, Part II
            . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
            Q4/03 SJ 3(4):420--439 (no commands)
            reviews three user-written commands (tabcount, makematrix,
            and groups) as different approaches to tabulation problems

            However, the paper is dispensible. Just start with the help file if interested.

            Here's an arbitrary example. (It is tidier in Stata than appears to be the case here.)

            Code:
            ssc desc groups
            ssc inst groups
            
            webuse nlswork
            
            . groups race msp nev_mar collgrad not_smsa , sepby(race)
            
            +--------------------------------------------------------------+
            | race msp nev_mar collgrad not_smsa Freq. Percent |
            |--------------------------------------------------------------|
            | 1 0 0 0 0 1705 5.98 |
            | 1 0 0 0 1 756 2.65 |
            | 1 0 0 1 0 381 1.34 |
            | 1 0 0 1 1 63 0.22 |
            | 1 0 1 0 0 2300 8.07 |
            | 1 0 1 0 1 717 2.51 |
            | 1 0 1 1 0 739 2.59 |
            | 1 0 1 1 1 189 0.66 |
            | 1 1 0 0 0 7086 24.85 |
            | 1 1 0 0 1 3761 13.19 |
            | 1 1 0 1 0 1857 6.51 |
            | 1 1 0 1 1 614 2.15 |
            |--------------------------------------------------------------|
            | 2 0 0 0 0 1268 4.45 |
            | 2 0 0 0 1 391 1.37 |
            | 2 0 0 1 0 157 0.55 |
            | 2 0 0 1 1 29 0.10 |
            | 2 0 1 0 0 1817 6.37 |
            | 2 0 1 0 1 462 1.62 |
            | 2 0 1 1 0 199 0.70 |
            | 2 0 1 1 1 36 0.13 |
            | 2 1 0 0 0 2304 8.08 |
            | 2 1 0 0 1 932 3.27 |
            | 2 1 0 1 0 402 1.41 |
            | 2 1 0 1 1 42 0.15 |
            |--------------------------------------------------------------|
            | 3 0 0 0 0 17 0.06 |
            | 3 0 0 0 1 4 0.01 |
            | 3 0 0 1 0 2 0.01 |
            | 3 0 0 1 1 1 0.00 |
            | 3 0 1 0 0 50 0.18 |
            | 3 0 1 0 1 6 0.02 |
            | 3 0 1 1 0 28 0.10 |
            | 3 0 1 1 1 4 0.01 |
            | 3 1 0 0 0 108 0.38 |
            | 3 1 0 0 1 36 0.13 |
            | 3 1 0 1 0 37 0.13 |
            | 3 1 0 1 1 10 0.04 |
            +--------------------------------------------------------------+
            Last edited by Nick Cox; 18 Jun 2015, 15:27.

            Comment


            • #7
              groups is very nice, thank you!

              Comment

              Working...
              X