Announcement

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

  • anycount

    Dear all

    I would like to count the number of values across 5 numeric variables. I tried using "anycount", but there is a wide range of values for me to list all of them in the option statement and the syntax wont allow me to specify range:

    2 syntaxes I tried below:

    egen sum_xy = anycount (x y z v w), value (1,2,3,4......1000)

    egen sum_xy = anycount (x y z v w), value (1-1000)

  • #2
    Code:
    numlist "1/1000"
    egen sum_xy= anycount(x y z v w), values(`r(numlist)')
    By the way, sum_xy sounds like a confusing, if not misleading, name for a variable that is calculated in this way. It's usually a good practice to give variables names that actually describe what they are/do.

    Comment


    • #3
      Thanks Clyde

      Comment


      • #4
        That code is going to imply a loop over 5 variables and another loop over 1000 distinct values.

        What is the real issue? e.g.: values at or below 1000? Are non-integers possible?

        Code:
        gen wanted = x <= 1000 
        
        foreach v in y z v w { 
             replace wanted = wanted + (`v' <= 1000)
        }
        where naturally you should specify the wanted condition directly. For example, if is really is an integer between 1 and 1000, then start with

        Code:
        gen wanted = inrange(x, 1, 1000) & floor(x) == x

        Comment


        • #5
          Thanks so much

          Comment

          Working...
          X