Announcement

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

  • Is there a “collapse (sum) sum_all=_all” function?

    Is there a way to collapse every single var in a dataset? (Some vars have missing data, but I think stata should still be able to sum?). All obs are numerical, no strings (type: byte, int, double, float)

    I’m on a boat and have no internet on computer, so can’t add example data at this time. My dataset is 666 vars and 67 0bs

    im using stata 17 for windows

    thanks for any advise !

  • #2
    Code:
     collapse (sum) *
    will do it. Just to be clear: this will create a dataset with a single observation, containing the totals of each variable. Not sure this is what you need. Presumably, you might want to do this by() some variables.

    I wasn't clear what you wanted: do you also want to generate a grand total of sums across variables?

    Comment


    • #3
      If there are any string variables then #2 will fail. A type-safe variation is:

      Code:
      sysuse auto
      ds, has(type numeric)
      collapse (sum) `r(varlist)'
      Edit: this will also only work with exactly one summary statistic, or else you will need to assign unique names for each variable for each statistic.
      Last edited by Leonardo Guizzetti; 17 Nov 2022, 07:35.

      Comment


      • #4
        Leonardo Guizzetti yes I had considered putting that into the code, but OP explicitly ruled out the need for it in #1: "All obs are numerical, no strings (type: byte, int, double, float)"

        Comment


        • #5
          That's fair, I was adding #3 for future readers who may not understand this subtlety that can only have numeric variables.

          Comment


          • #6
            Thank you both! Both great suggestions!

            I had data in wide format and had calculated several squared residuals, I needed help to sum them. So I ended up using #2 and hadn't figured out that * or varnamestub_* did the trick!
            Thanks a lot!

            Comment

            Working...
            X