Announcement

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

  • Rowtotal with by

    Hi,

    I am using the function ‘rowtotal' to add three variable. However, I found that I can’t use ‘by’ with rowtotal. Is there any other way of adding the three variables by ArrangementNumber.

    Code:
    . egen TotalConditions = rowtotal (NumOfSB1 NumOfSB2 NumOfSB3), by (ArrangementNumber)
    egen ... rowtotal() may not be combined with by
    Here is what my data looks like:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int ArrangementNumber float(NumOfSB1 NumOfSB2 NumOfSB3)
    117 13 0 3
    117 13 0 3
    117 13 0 3
    117 13 0 3
    117 13 0 3
    117 13 0 3
    117 13 0 3
    117 13 0 3
    117 13 0 3
    117 13 0 3
    117 13 0 3
    117 13 0 3
    117 13 0 3
    117 13 0 3
    117 13 0 3
    117 13 0 3
    117 13 0 3
     77  4 0 0
     77  4 0 0
     77  4 0 0
     77  4 0 0
     77  4 0 0
     77  4 0 0
     77  4 0 0
     77  4 0 0
     77  4 0 0
     77  4 0 0
     77  4 0 0
     77  4 0 0
     77  4 0 0
     77  4 0 0
     77  4 0 0
     77  4 0 0
     77  4 0 0
     77  4 0 0
     77  4 0 0
     77  4 0 0
     77  4 0 0
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
     84 30 0 3
    end
    I look forward to your guidance.

    Best regards,
    Imran Khan

  • #2
    Imran:
    a possible work-around is:
    Code:
    egen TotalConditions = rowtotal (NumOfSB1 NumOfSB2 NumOfSB3) if ArrangementNumber!=.
    Kind regards,
    Carlo
    (Stata 15.1 SE)

    Comment


    • #3
      Why should other variables make any difference to a row total?

      Perhaps what you want is to add across rows and also within subsets of observations, either way first. For that use rowtotal() and then total() with by(), or the other way round.

      Two commands in sequence, not one.

      Comment


      • #4
        Dear Carlo,

        Many thanks for your suggestion. I ran the suggested command and it exactly did what I wanted.
        Just to understand the commands, I ran the same command without ‘if’.
        To my surprise, both the commands produced the same output.

        Code:
        egen TotalConditions = rowtotal (NumOfSB1 NumOfSB2 NumOfSB3) if ArrangementNumber!=. egen TotalConditions1 = rowtotal (NumOfSB1 NumOfSB2 NumOfSB3)
        Code:
         * Example generated by -dataex-. To install: ssc install dataex clear input int ArrangementNumber float(NumOfSB1 NumOfSB2 NumOfSB3 TotalConditions TotalConditions1) 117 13 0 3 16 16 117 13 0 3 16 16 117 13 0 3 16 16 117 13 0 3 16 16 117 13 0 3 16 16 117 13 0 3 16 16 117 13 0 3 16 16 117 13 0 3 16 16 117 13 0 3 16 16 117 13 0 3 16 16 117 13 0 3 16 16 117 13 0 3 16 16  77  4 0 0  4  4  77  4 0 0  4  4  77  4 0 0  4  4  77  4 0 0  4  4  77  4 0 0  4  4  77  4 0 0  4  4  77  4 0 0  4  4  77  4 0 0  4  4  77  4 0 0  4  4  77  4 0 0  4  4  77  4 0 0  4  4  77  4 0 0  4  4  77  4 0 0  4  4  77  4 0 0  4  4  77  4 0 0  4  4  77  4 0 0  4  4  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33  84 30 0 3 33 33 end
        As far as I guess, it is because I don’t have any missing observations in the data. But if this is the case, I am wondering how could Stata/MP 15.0 knows that it has to add the rows by the variable 'Arrangement Number' Could you please clear this confusion for me? Best regards, Imran Khan.

        Comment


        • #5
          Dear Nick,

          Many thanks for your reply.

          I think I didn’t explain my question clearly in the first instance.
          Actually, I want to add the three variables (NumOfSB1 NumOfSB2 NumOfSB3) across rows by the grouping variable (ArrangementNumber).

          Kindly let me know if I have been able to make the question clear.

          Best regards,
          Imran Khan.


          Comment


          • #6
            The sum a + b + c doesn't depend on what d is, so I am at a loss to know what you're missing here. The reason by() is not implemented there is that it could make no possible difference.

            Why not give a worked example showing some of your example data to make clear what you want and explain how it differs from what you can calculate already?
            Last edited by Nick Cox; 13 Oct 2017, 06:31.

            Comment


            • #7
              Dear Nick,

              Many thanks for your reply.

              I got it now. This is such a common-sense for which I was getting confused.

              Best regards,
              Imran Khan.

              Comment


              • #8
                Thanks for the closure. Happens to us all.

                Comment


                • #9
                  Imran:
                  I'm probably too late for the party, but you got the same results because no -ArrangementNumber- was missing in your dataset.
                  Kind regards,
                  Carlo
                  (Stata 15.1 SE)

                  Comment


                  • #10
                    Many thanks for your reply, Carlo.

                    Best regards,
                    Imran Khan

                    Comment

                    Working...
                    X