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