Announcement

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

  • How to count number of unique values in a given row?

    Hi Stata Lovers,


    I am working with a dataset. Below is e.g. of the dataset.

    There are 20 variables for each observation. Each of these 20 variables can have a value ranging from 0 to 5.

    I want to create a new variable xxx to indicate how many of the 1 to 20 subjects variables have a value of 3.

    For e.g. of the total 20 subjects variables for a given observation, if the following 5 variables viz. subject1, subject3, subject6, subject9, and subject12 have a value of 3 (may be for the 100th observation), I want variable xxx to reflect a value of 5 (indicating that of the total 20 subjects variables- there are 5 subjects variables having a value==3).

    input float(subject1 subject2 subject3 subject4 subject5 subject6 subject7 subject8 subject9 subject10 subject11 subject12 subject13 subject14 subject15 subject16 subject17 subject18 subject19 subject20)
    2 2 2 . . . . . . . . . . . . . . . . .
    2 2 2 . . . . . . . . . . . . . . . . .
    4 2 2 . . . . . . . . . . . . . . . . .
    2 2 2 . . . . . . . . . . . . . . . . .
    3 2 2 . . . . . . . . . . . . . . . . .
    2 2 . . . . . . . . . . . . . . . . . .
    2 2 2 . . . . . . . . . . . . . . . . .
    2 2 2 . . . . . . . . . . . . . . . . .
    3 2 3 2 . . . . . . . . . . . . . . . .
    2 2 2 . . . . . . . . . . . . . . . . .
    4 2 3 3 3 . . . . . . . . . . . . . . .
    2 2 2 . . . . . . . . . . . . . . . . .
    2 3 2 2 3. . . . . . . . . . . . . . . .
    2 2 . . . . . . . . . . . . . . . . . .
    2 2 2 . . . . . . . . . . . . . . . . .
    2 2 2 2 . . . . . . . . . . . . . . . .
    2 2 2 . . . . . . . . . . . . . . . . .
    2 2 2 2 . . . . . . . . . . . . . . . .
    2 2 2 . . . . . . . . . . . . . . . . .
    2 2 2 . . . . . . . . . . . . . . . . .
    2 2 2 . . . . . . . . . . . . . . . . .
    3 2 3 3 . . . . . . . . . . . . . . . .
    2 2 2 . . . . . . . . . . . . . . . . .
    2 2 2 . . . . . . . . . . . . . . . . .
    2 2 2 . . . . . . . . . . . . . . . . .
    2 2 2 . . . . . . . . . . . . . . . . .
    2 2 2 . . . . . . . . . . . . . . . . .
    2 2 2 2 . . . . . . . . . . . . . . . .
    2 . . . . . . . . . . . . . . . . . . .
    2 2 2 2 2 2 . . . . . . . . . . . . . .









  • #2
    See the help for egen and in particular the help for its functions whose names start with any.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      See the help for egen and in particular the help for its functions whose names start with any.
      Thank you again.

      Comment

      Working...
      X