Announcement

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

  • Row- totals, means and counts with an if condition

    Dear Stata users,

    I have a set of variables lets say a, b, c, d, e, f, g, h

    I want to generate 3 variables that capture the row -mean, -total and -count of these variables for values >0 and then another 3 variables that capture the row -mean, -total and -count of these variables for values <0. Can anyone help me how to do this?


    Many thanks for your kind help.

  • #2
    I find your question quite unclear - when you say "values>0", what values are you talking about? if you mean the variables a-h, do you mean that if some are positive for a specific observation while others are negative, then you want separate summary statistics for that set of variables? what if the next observation has a different subset of positive values? also, what if a value is equal to 0?

    or do you mean that if some other variable has a positive value?

    Comment


    • #3
      Sorry for all the unclarities.
      When reffering to the row-totals lets say, I want to generate one variable that sums only the positive values (values>0) of the variables a-h. In other words the new variable captures the row sum of only the positive values of variables a-h.
      Then I want to generate another variable that sums only the negative values of the variales a-h.
      Is this clearer now?

      Something similar to this: https://www.stata.com/statalist/arch.../msg00088.html








      Comment


      • #4
        egen gives one answer; otherwise I think you need a loop. So you might as well use a loop. Watch out for missings either way.

        Code:
        gen sumpositive = 0
        gen sum = 0
        
        quietly foreach v in a b c d e f g h {
              replace positive = positive + `v' if `v' > 0  & `v' < .
              replace sum = sum  + `v' if `v' < .
        }
        The last part is set as an exercise!
        Last edited by Nick Cox; 06 Dec 2017, 06:21.

        Comment


        • #5
          Dear Nick, many thanks.
          One more question, how to write the loop for the count and the mean? Here I am still confused cause normally one would need egen, no?
          Thanks so much.

          Comment


          • #6
            No conflict here: egen when it applies to such problems uses a loop over variables, but otherwise you need to write your own loop.

            But I have given you a loop already, so otherwise I don't understand what new thing you're asking here.

            For much more detail, see http://www.stata-journal.com/sjpdf.h...iclenum=pr0046 and/or mentions of pr0046 on this site.

            Comment


            • #7
              In #4 one variable should be either -positive- or -sumpositive- consistently.

              Comment


              • #8
                Dear Nick,
                many thanks. Now all works!

                Comment

                Working...
                X