Announcement

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

  • Number of distinct values across variables

    Hello,

    I have what feels like a very elementary problem, yet so far I have failed to find a proper solution to it. For various family members, a closed question on ancestry was asked and I simply would like to construct a variable that counts the number of distinct values across those variables for each respondent, disregarding missings. For example, if the variable is missing for 2 family members and has the value 4 in the other cases, the new variable should be 1, if the row of values is 3 3 4 3 5 . . , it should be 3 etc.

    Does anyone know an easy way to do this? Thanks!

    Best,

    Viktor

  • #2
    See rownvals in help egenmore

    rownvals(numvarlist) [ , missing ] returns the number of distinct values in each observation for a set
    of numeric variables numvarlist. Thus if the values in one observation for five numeric variables
    are 1, 1, 2, 2, 3 the function returns 3 for that observation. Missing values, i.e. any of . .a
    ... .z, are ignored unless the missing option is specified. (Stata 9 required.)
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      Carole is naturally right. There is a write-up of this problem within http://www.stata-journal.com/sjpdf.h...iclenum=pr0046 Section 7.

      Fond though I am of that function, it's worth stressing another way to do it too. reshape, work out the number of distinct values within groups and reshape back.

      Code:
      clear 
      input y1 y2 y3 
      1    1  1
      1    2  1
      1    3  1
      2    1  1
      2    2  1
      2    3  1
      3    1  1
      3    2  1
      3    3  1
      end 
      gen id = _n 
      reshape long y, i(id) 
      egen tag = tag(y id) 
      egen nvals = total(tag), by(id)
      drop tag 
      reshape wide y, i(id)   
      list, sep(0)  
      
           +---------------------------+
           | id   y1   y2   y3   nvals |
           |---------------------------|
        1. |  1    1    1    1       1 |
        2. |  2    1    2    1       2 |
        3. |  3    1    3    1       2 |
        4. |  4    2    1    1       2 |
        5. |  5    2    2    1       2 |
        6. |  6    2    3    1       3 |
        7. |  7    3    1    1       2 |
        8. |  8    3    2    1       3 |
        9. |  9    3    3    1       2 |
           +---------------------------+

      Comment


      • #4
        Thank you very much for the quick and useful answers, much appreciated!

        Comment

        Working...
        X