Announcement

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

  • Finding sum per row based on conditions

    Members,

    I have a dataset with multiple columns (var1, var2, var3, etc.) with missing observations. Per row, I want to find the sum of the values for var1, var2, var3, etc. that come after a missing value. For instance, if var1 is missing, I would only take the sum of var2, var3, etc. If var2 is missing, I would only take the sum of var3, var4, etc.

    I would appreciate any assistance with this!

    Thanks,
    A

  • #2
    What if var1 and var3 are both missing?

    Also, please review the advice on providing example data given at #2 in your previous topic.

    Comment


    • #3
      The egen function rowtotal() ignores missings. If it is not what you want, I think we need a precise statement of how your rules differ from its rules, preferably with a data example showing what it does and what you want and how they differ.

      Comment


      • #4
        Thank you for your responses, William and Nick.

        Here is the data I'm working with:

        Code:
        clear
        input float(v1 v2 v3 v4 v5 v6)
        .61 .29 . . . .12
        .52 .63 . . . .11
        .72 .79 .84 .43 .33 .
        .57 .37 .22 . .71 .
        end
        tempfile dataset1
        save `dataset1'
        What I would like to do is add the values per row that come after a missing value. It should look something like this:

        Code:
        clear
        input float(v1 v2 v3 v4 v5 v6 Total)
        .61 .29 . . . .12 .12
        .52 .63 . . . .11 .11
        .72 .79 .84 .43 .33 . 3.11
        .57 .37 .22 . .71 . .71
        end
        tempfile dataset1
        save `dataset1'
        If no values are missing, then I would add all values per row. If the last value is missing then I would add all values prior to that. If there is a jump (var4 is missing) then I would add only the values that come after var4. If var3 and var5 are missing, then I would only use var6.

        I hope this clarifies my question a bit more. I would definitely appreciate any assistance!

        A

        Comment


        • #5
          What a bizarre set of rules... Perhaps

          Code:
          egen wanted = rowtotal(v?)
          
          replace wanted = max(var5, 0) + var6 if missing(var4)
          
          replace wanted = var6 if missing(var3) & missing(var5)

          Comment


          • #6
            Thank you so much Nick! I really appreciate your help.

            Comment

            Working...
            X