Announcement

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

  • egen's anycount() incorrectly counting

    I have a list of 23 variables that can have a value between 1 and 12. I would like to get the number of variables in a given row that have a certain value. I have tried using egen's anycount but this has not produced accurate results. For example
    Code:
    local values    1 2 3 4 5 6 7 8 9 10 11 12
    
    foreach i of local values {
            egen nbo_var_`i' = anycount(var_1 var_2 var_3 var_4 var_5 ///
                                                          var_6 var_7 var_8 var_9 var_10 ///
                                                          var_11 var_12 var_13 var_14 var_15 ///
                                                          var_16 var_17 var_18 var_19 var_20 ///
                                                          var_21 var_22 var_23), v(`i')
    }
    so if an observation has two variables between the above varlist (i.e. var_*) that have a value of 3, nbo_var_3 ought to be 2.

    However, each of the variables generated above in my dataset has between one and three inaccuracies. The inaccuracies always count one more value than they ought to. The code below ought to be able to replicate the same thing but if at all possible, I would like to know why anycount is not working as specified above. All of my variables are integers.


    Code:
    local vars 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
    local values 1 2 3 4 5 6 7 8 9 10 11 12
    foreach i of local vars {
        foreach j of local values {
            g var_`i'_val_`j' = (var_`i'  == `j')
        }
    }
    
    foreach j of local values{
         egen nbo_var_`j'_new = rowtotal(var_1_val_`i' var_2_val_`i' var_3_val_`i' var_4_val_`i' var_5_val_`i' ///
                                              var_6_val_`i' var_7_val_`i' var_8_val_`i' var_9_val_`i' var_10_val_`i' ///
                                              var_11_val_`i' var_12_val_`i' var_13_val_`i' var_14_val_`i' var_15_val_`i' ///
                                              var_16_val_`i' var_17_val_`i' var_18_val_`i' var_19_val_`i' var_20_val_`i'  ///
                                              var_21_val_`i' var_22_val_`i' var_23_val_`i')
    }

  • #2
    Example code does not work

    Code:
    . local vars 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
    
    . local values 1 2 3 4 5 6 7 8 9 10 11 12
    
    . foreach i of local vars {
      2.     foreach j of local values {
      3.         g var_`i'_val_`j' = (var_`i'  == `j')
      4.     }
      5. }
    var_1 not found
    r(111);
    
    end of do-file
    Which version of Stata are you using?

    Best
    Daniel

    Comment


    • #3
      Using stata 15.0

      Comment


      • #4
        Please show us some data for which you make this claim. Your example can't be used to replicate your experience without the original data.

        Comment


        • #5
          Thank you very much for your willingness to help but I unfortunately can't share any of my data. Trying to create a sandbox version now but I'm afraid that the problem wouldn't replicate. Am I correct in that, as I've described my data, the two examples above ought to return similar outputs or are there properities of anycount that I am not considering?

          Comment


          • #6
            This function goes back to 1999 at least (as community-contributed and under another name) but it's been in official Stata since version 7, so I am rather in denial about there being a bug -- particularly as I was the original author....

            More importantly, it's also hard to imagine what could be possibly be wrong or problematic about the data either. I would check that you really do have integers there. For example, you should be able to compress all such variables to byte. At the same time, values that aren't exactly integers would result in undercounts, so I am more than usually dependent on an example to replicate.

            Comment


            • #7
              Hi Nick,

              Thank you very much for your reply. It was 100 % a problem of my own doing not a reflection of the function.

              Comment


              • #8
                Jack: Closure in this way is highly appreciated.

                Comment

                Working...
                X