Announcement

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

  • Simple way to calculate percentage of '.a' per case?

    Could anyone offer advice on the following

    (i) generating a variable which displays the percentage of data with a specific value (in this case .a) within a range of variables - as var6 does below - i.e. the percentage of .a values in vars 1-5 for each case.

    (ii) I'm also looking for a way to count .a values, but taking the .b cells out of the equation so that for each case the .b variables are essentially ignored and the new variable becomes the percentage of .a values in vars 1-5 (where vars don't equal .b) as is done in var7.
    ID var1 var2 var3 var4 var5 var6 var7
    1 10 18 251 21 0 0 0
    2 8 5 45 2 .a 20 20
    3 .b .b .a 6 16 20 33.33
    4 .a .a .a .a 1 80 80
    5 13 .a .b 81 11 20 25
    6 25 .a .a 12 199 40 40
    7 16 6 34 15 78 0 0
    Does that make sense? Any thoughts much appreciated!
    Last edited by Matthew Breckons; 24 Nov 2017, 07:24. Reason: left out info

  • #2
    Here is how you create var6

    Code:
    generate var6 = 0
    foreach var of varlist var1-var5 {
        replace var6 = var6 + (`var' == .a)
    }
    replace var6 = (var6/5)*100
    Obviously, you can use that approach to count the number of .b occurrences in the variables, then subtract that from 5, when dividing.

    rcount(), part of egenmore (from SSC) should do this, too, but does not because it handles "modern" missing values in a wrong way. I will notify Nick Cox, so he can change that.

    Best
    Daniel
    Last edited by daniel klein; 24 Nov 2017, 07:57.

    Comment


    • #3
      Thank you very much Daniel,

      It's not essential but if I were to be using more variables than var1-var5 do you know how I could modify
      replace var6 = (var6/5)*100 to reflect 'n' variables instead of manually entering the 5?

      Comment


      • #4
        On #3 I see Daniel's point, but updating rcount() is perhaps more than I ever want to put on a to-do list. It's a 2001 program and has been out-of-date for some years without anyone noticing before.

        Comment


        • #5
          Matthew,

          Code:
          unab myvarlist : var1-var5
          local nvars : word count `myvarlist'
          
          generate var6 = 0
          foreach var of local myvarlist {
              replace var6 = var6 + (`var' == .a)
          }
          replace var6 = (var6/`nvars')*100
          will do.

          Best
          Daniel

          Comment


          • #6
            Another way to do it:

            Code:
            gen total = 0 
            gen count = 0 
            foreach var of var var1-var5 {
                replace total = total + (`var' == .a)
                replace count = count + 1       
            }
            gen pc = 100 * total/count

            Comment


            • #7
              That's fantastic - thanks so much for your help Daniel.

              Comment

              Working...
              X