Announcement

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

  • Does gen,sum() and egen,total() work differently when combined with bysort?

    Hi,

    Please consider the following data:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(distid hospid number)
    1 1 10
    1 2 10
    1 3 10
    2 2 12
    2 1 12
    2 3 12
    3 2 13
    3 3 13
    3 1 13
    end
    I used sum() and total() combined with bysort but got different results. Do sum() and total() work differently when combined with bysort?

    Code:
    sort distid hospid
    
    .
    . by distid: gen sum=sum( number )
    
    .
    . by distid: egen total=total( number )
    and it produced the following:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(distid hospid number sum total)
    1 1 10 10 30
    1 2 10 20 30
    1 3 10 30 30
    2 1 12 12 36
    2 2 12 24 36
    2 3 12 36 36
    3 1 13 13 39
    3 2 13 26 39
    3 3 13 39 39
    end

  • #2
    Titir:
    -sum()- produces the running sum of the values observed for each -id-;
    -total- from -egen- gives back a constant=to the sum of the values observed for each -id-.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Originally posted by Carlo Lazzaro View Post
      Titir:
      -sum()- produces the running sum of the values observed for each -id-;
      -total- from -egen- gives back a constant=to the sum of the values observed for each -id-.
      Thanks Carlo!
      I was really confused by this. Much appreciated

      Comment


      • #4
        Code:
        help sum()
        explains. Tip: firing up help with a function name and parentheses () takes you straight to specific help.

        Comment

        Working...
        X