Announcement

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

  • summing occurrence of numeric variables

    Hello,

    I have 10 numeric variables: offense1-offense10. Each one can take a specific value of between 1 and 700. I want to create 5 variables that sums the total of 5 specific numeric observations across all 10 of the offense variables.
    For example, if offense1, offense2, and offense8=20, I want to create a new variable indicating that the value 20 occurred 3 times. Obviously egen rowtotal would give me the value 60, but I'm looking for occurrences, not totals.

    Thanks!

  • #2
    Perhaps something like this will help.
    Code:
    foreach val in 20 42 666 {
        generate n`val' = 0
        foreach var of varlist offense* {
            replace n`val' = n`val' + `var'==`val'
        }
    }

    Comment


    • #3
      See also the functions of egen whose names start with any.

      Comment


      • #4
        With Nick's excellent advice at hand, I'd replace lines 2-5 of the code in post #2 with
        Code:
             egen n`val' = anucount(offense*), values(`val')

        Comment


        • #5
          In #4 anucount() is a typo for anycount(), in case that's not apparent.

          Comment


          • #6
            Thanks so much, Nick and William. I had looked through egen help and completely missed anycount. That's what I needed.

            Comment


            • #7
              It was originally (1999) called neqany() -- and that name is now undocumented, but still works. The function under its present name has been available since Stata 9.

              Comment

              Working...
              X