Announcement

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

  • How to use collapse (sum) + missing values

    Hi,

    I have a question regarding collapse(sum) command in STATA. Is it okay if I have three observations falling in the same category say "a" such as: 6, . , 3 and collapse(sum) gives as a result 9? Hence, it treats the missing value as 0 in adding them up.

    I was thinking would it more appropriate if it adds the 6 and 3 but it also leaves the missing value not added like:
    a 9
    a .

    If yes, do you know how could this be done?

    Thanks and sorry in advance if my question is not structured properly and is not clear enough
    Last edited by Tilo Gashi; 25 May 2016, 16:43. Reason: collapse sum missing values

  • #2
    Aggregate sum() functions in Stata treat missing values as zero. This is typically the most useful approach to missing values in situations where grups of numbers are being added. However, if you want to treat missing values separately, you can do so. For example:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str1 category float summand
    "a" .
    "a" 2
    "a" .
    "a" 1
    "b" 3
    "b" .
    "b" 7
    end
    
    gen byte summand_missing = missing(summand)
    collapse (sum) summand, by(category summand_missing)
    replace summand = . if summand_missing
    
    list, sepby(category)

    Comment


    • #3
      Thanks a lot Clyde Schechter!

      Comment

      Working...
      X