Announcement

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

  • rowsum of many columns

    Dear All, I found this question here (http://bbs.pinggu.org/thread-6390737-1-1.html). Suppose that I have 4914 variables (say, v1, v2, ..., v4914). I'd like to obtain row sum of every 26 variables (s1=v1+...+v26; s2=v27+...+v52; ... ..., s189=v4889+...+v4914). Since the data set is too large, so there is no representative sample here. Thanks for any suggestions.
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    I believe this will do it:

    Code:
    //    GENERATE A DEMONSTRATION DATA SET
    clear*
    set maxvar 10000
    set obs 10
    set seed 1234
    forvalues i = 1/4914 {
        gen v`i' = runiform()
    }
    
    //    CODE TO CALCULATE THE SUMS
    //        INITIALIZE LOOP PARAMETERS
    local g = 1
    local first = 1
    local last = 26
    
    //        LOOP OVER GROUPS OF 26 VARIABLES
    //        SUMMING AS WE GO
    while `first' < 4914 {
        gen s`g' = 0
        forvalues i = `first'/`last' {
            replace s`g' = s`g' + v`i'
        }
        local ++g
        local first = `first' + 26
        local last = `last' + 26
    }

    Comment


    • #3
      Dear Clyde, Many thanks for your solution.
      Ho-Chuan (River) Huang
      Stata 19.0, MP(4)

      Comment


      • #4
        Below loop would also properly serve for using rowtotal.
        Code:
        order v*, seq
        forval g=1/189 {
        egen s`g' = rowtotal(v`=26*`g'-25' - v`=26*`g'') // treating missing as 0
        }
        Last edited by Romalpa Akzo; 22 May 2018, 03:54.

        Comment


        • #5
          Hi Romalpa, Thanks a lot for offering this interesting/concise answer.

          Ho-Chuan (River) Huang
          Stata 19.0, MP(4)

          Comment

          Working...
          X