Announcement

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

  • conditional rowtotal

    Hello, I am trying to sum across columns only the 1s.

    I have tried
    egen pri_sum=rowtotal(pri_q1-pri_q7) if (pri_*) < 2

    and

    egen pri_sum=rowtotal(pri_q1-pri_q7) if pri_* < 2


    I think that "if" just does not work for conditioning on row total. Is there another option?


    ​​​​​​
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(id pri_q1 pri_q2 pri_q3 pri_q4 pri_q5 pri_q6 pri_q7)
     1 1 1 1 1 2 2 1
     2 1 1 1 1 1 2 1
     3 2 2 2 2 1 2 2
     4 1 1 1 1 1 3 1
     5 1 2 2 1 2 2 2
     6 1 1 1 2 2 1 2
     7 1 1 1 1 2 1 2
     8 1 1 1 1 3 2 2
     9 1 2 1 3 3 1 1
    10 1 1 1 1 1 1 1
    11 1 1 1 1 1 1 1
    12 1 1 1 1 1 1 2
    13 1 1 1 1 1 1 1
    14 1 2 2 2 1 2 2
    15 1 1 1 1 1 1 1
    16 1 2 2 1 3 1 1
    17 1 2 2 2 1 1 2
    18 1 2 2 2 1 1 1
    19 1 2 2 2 1 2 2
    20 1 1 1 1 1 2 2
    21 1 1 1 3 1 2 1
    22 1 2 2 1 1 1 2
    23 1 1 1 1 2 2 2
    24 1 1 1 1 1 1 2
    25 1 1 1 1 2 2 2
    26 1 1 2 1 2 2 2
    27 1 1 1 3 3 1 3
    28 1 2 2 1 1 2 1
    29 1 1 1 3 1 1 2
    30 2 2 2 2 2 2 2
    31 1 1 1 1 1 2 1
    32 1 1 1 1 1 1 1
    33 1 2 1 1 2 2 2
    34 1 2 2 1 2 2 2
    35 1 1 1 2 1 2 2
    36 1 1 2 2 1 2 2
    37 1 3 1 2 2 2 2
    38 1 1 1 1 1 1 2
    39 1 1 1 1 3 1 2
    40 1 1 1 1 2 1 2
    end

  • #2
    If the following is what you want to do:

    Code:
    . egen sumones = rcount( pri_q1- pri_q7), cond(@<2)
    
    . list, clean
    
           id   pri_q1   pri_q2   pri_q3   pri_q4   pri_q5   pri_q6   pri_q7   sumones  
      1.    1        1        1        1        1        2        2        1         5  
      2.    2        1        1        1        1        1        2        1         6  
      3.    3        2        2        2        2        1        2        2         1  
      4.    4        1        1        1        1        1        3        1         6  
      5.    5        1        2        2        1        2        2        2         2  
      6.    6        1        1        1        2        2        1        2         4  
      7.    7        1        1        1        1        2        1        2         5  
      8.    8        1        1        1        1        3        2        2         4  
      9.    9        1        2        1        3        3        1        1         4  
     10.   10        1        1        1        1        1        1        1         7  
     11.   11        1        1        1        1        1        1        1         7  
     12.   12        1        1        1        1        1        1        2         6  
     13.   13        1        1        1        1        1        1        1         7  
     14.   14        1        2        2        2        1        2        2         2  
     15.   15        1        1        1        1        1        1        1         7  
     16.   16        1        2        2        1        3        1        1         4  
     17.   17        1        2        2        2        1        1        2         3  
     18.   18        1        2        2        2        1        1        1         4  
     19.   19        1        2        2        2        1        2        2         2  
     20.   20        1        1        1        1        1        2        2         5  
     21.   21        1        1        1        3        1        2        1         5  
     22.   22        1        2        2        1        1        1        2         4  
     23.   23        1        1        1        1        2        2        2         4  
     24.   24        1        1        1        1        1        1        2         6  
     25.   25        1        1        1        1        2        2        2         4  
     26.   26        1        1        2        1        2        2        2         3  
     27.   27        1        1        1        3        3        1        3         4  
     28.   28        1        2        2        1        1        2        1         4  
     29.   29        1        1        1        3        1        1        2         5  
     30.   30        2        2        2        2        2        2        2         0  
     31.   31        1        1        1        1        1        2        1         6  
     32.   32        1        1        1        1        1        1        1         7  
     33.   33        1        2        1        1        2        2        2         3  
     34.   34        1        2        2        1        2        2        2         2  
     35.   35        1        1        1        2        1        2        2         4  
     36.   36        1        1        2        2        1        2        2         3  
     37.   37        1        3        1        2        2        2        2         2  
     38.   38        1        1        1        1        1        1        2         6  
     39.   39        1        1        1        1        3        1        2         5  
     40.   40        1        1        1        1        2        1        2         5  
    
    .
    type from command line

    Code:
    . findit egenmore
    and install the user contributed package -egenmore-, it will give you access to the above function.

    Comment


    • #3
      Exactly! Thanks.

      Comment


      • #4
        You are welcome.

        Can you also do something for me, if you have installed the -egenmore- ?

        Can you type from the command line -which _gxtile- and paste here the message that Stata returns to you? Like this:

        Code:
        . which _gxtile
        c:\ado\plus\_\_gxtile.ado
        *! _gxtile version 1.2 UK 08 Mai 2006
        ( I am wondering whether I have the most recent version of the above function, because it is generating some errors.)

        Comment


        • #5
          That's the version / date that I have, and I just installed -egenmore- last month.

          Code:
          . which _gxtile
          c:\ado\plus\_\_gxtile.ado
          *! _gxtile version 1.2 UK 08 Mai 2006

          Comment


          • #6
            Thank you David Benson !

            I encountered a mystery: the _gxtile works on Stata 11, 12, 13, but generates an error under Stata 15.

            So this is why I am not able to go for the natural solution of just installing the most recent version overwriting whatever I have (rather than wondering whether I have the most recent version or not)...

            I am afraid that I will overwrite the mystery if I just install the most recent version .

            Comment


            • #7
              Jake, in this case because the condition involved is matching the integer 1, also the following will do the trick:

              Code:
              egen sumones = anycount( pri_q1- pri_q7), values(1)
              and the -egen, anycount- function is part of the standard Stata, so there is no need to install anything if you have not installed it yet.

              Comment


              • #8
                I installed the egenmore without problems with Stata 15. Thanks for the information about anycount also.

                Comment

                Working...
                X