Announcement

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

  • Sum across many columns or many rows?

    Dear Statalisters,

    I have a dataset with variables var_1:var_200 as well as a variable id that takes values 1:200 as well, so that the total dataset has 200 rows and 201 columns including the "id" variable.

    Now I would like to sum first across all rows, and secondly across all but the first column (I could drop the first if necessary). How can I do that?

    I've tried summing across the columns with "egen example = rowtotal()", but would need to type each of the 200 variables there? Or can I at least tell Stata more succintly to sum all of var_1 through var_200?

    Thank you so much and best regards,
    PM

  • #2
    Peter:
    if your variabes are actually named -var_1-, -var_2- and so on, you can use the wildcard symbol (*) to speed things up:
    Code:
    . set obs 10
    number of observations (_N) was 0, now 10
    
    . g var_1=runiform()
    
    . g var_2=runiform()
    
    . egen sum=rowtotal(var_*)
    
    . list
    
         +--------------------------------+
         |    var_1      var_2        sum |
         |--------------------------------|
      1. | .3488717   .2047095   .5535812 |
      2. | .2668857   .8927587   1.159644 |
      3. | .1366463   .5844658   .7211121 |
      4. | .0285569   .3697791    .398336 |
      5. | .8689333   .8506309   1.719564 |
         |--------------------------------|
      6. | .3508549   .3913819   .7422367 |
      7. | .0711051   .1196613   .1907664 |
      8. |  .323368   .7542434   1.077611 |
      9. | .5551032   .6950234   1.250127 |
     10. |  .875991   .6866152   1.562606 |
         +--------------------------------+
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      1. What Peter want is:
      Code:
      egen example1=rowtotal(var_1-var_200)
      Note the dash(-). Of course, you have to check and type exactly the name of the second variable (after id, i.e var_1) and the last (i.e var_200).

      2. The more convenient code might be:
      Code:
      ds id, not
      egen example2=rowtotal(`r(varlist)')
      which requires the name of the very first variable (i.e id) only.

      Comment


      • #4
        Thank you Carlo and Romalpha!

        Both the version with the wildcard and the one with the dash are working now!

        All best, PM

        Comment

        Working...
        X