The SUM function will aggregate over the observations for given set of values of the grouping variables. Normally you would want that OUTSIDE the case statement. In this example the case statement will assign a number to each of the observations and then the SUM function will add them up. There might be situations where you would put the sum function inside the case statement, but this example does not look like one.
... View more