Announcement

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

  • count non-zero values in descriptive statistics

    Dear Stata users,

    Is there any way to count non-zero values in addition to mean, s.d., max and min in descriptive statistics?

    Cheers,
    Azreen.

  • #2
    See the help file for count. And from there, see the help file for if.

    Comment


    • #3
      Thanks Joseph. Basically I want to get a descriptive statistics table with mean, s.d., max, min and count of zeros or non-zero values (either way) for each of my variables in my dataset. Is there any way to combine these?

      Cheers,
      Azreen.

      Comment


      • #4
        Azreen:
        as Joseph recommended:
        Code:
        input sandwiches
        1. 0
          2. 1
          3. 2
          4. 3
          5. 4
          6. end
        sum sandwiches
        count if sandwiches!=0
        Kind regards,
        Carlo
        Kind regards,
        Carlo
        (Stata 19.0)

        Comment


        • #5
          This is a non-standard output, but it could occationally be useful. I woud probably stick with Carlo's solution, whith the exception that I would use:

          Code:
          count if sandwiches != 0 & !missing(sandwiches)
          to guard agains missing values. However nothing stops you from wrapping this all up in your own Stata program. Something like so:

          Code:
          clear all
          
          program define sumwith0s, rclass
              syntax varlist(numeric) [if] [in]
              marksample touse, novarlist
              
              tempname res
              local k : word count `varlist'
              matrix `res' = J(`k',6,.)
              matrix colnames `res' = Obs Non0 Mean Std_Dev Min Max
              matrix rownames `res' = `varlist'
              local i = 1
              foreach var of local varlist {
                  qui count if `var' != 0 & `touse' & !missing(`var')
                  local non0 = r(N)
                  qui sum `var' if `touse'
                  matrix `res'[`i',1] = r(N), `non0', r(mean), r(sd), r(min), r(max)
                  local i = `i' + 1
              }
              matlist `res', underscore
              return matrix res = `res'
          end
              
          
          input sandwiches steaks
          0  0
          1  .
          2  0
          3  5
          4 10
          end
          
          sumwith0s sandwiches steaks
          ---------------------------------
          Maarten L. Buis
          University of Konstanz
          Department of history and sociology
          box 40
          78457 Konstanz
          Germany
          http://www.maartenbuis.nl
          ---------------------------------

          Comment


          • #6
            Maarten:
            this morning I was probably too hungry for figuring out missing sandwiches!
            Congrats' on your catch about missing ones and as always smart solution.
            Kind regards,
            Carlo
            Kind regards,
            Carlo
            (Stata 19.0)

            Comment


            • #7
              A customised program is the best solution here. Nevertheless I draw attention to inspect.

              (It's been a hobby-horse of mine for some time that the best bits of inspect should just be rolled into codebook, but while that has yet to happen, inspect is good for this purpose.)

              Comment

              Working...
              X