Solved
Contributor
Posts: 22

# Proc Sql: Is it possible to sum by group and also sum overall at the same time?

Hi all,

I am doing a simple Sum using the Group By statement and I got what I wanted -- the total members in each state and total spend in each state.

Now, I was thinking if it's possible to add another row that shows the overall national total (sum of all states).

Below is my simplified code.

proc sql;

create table SUM_BY_STATE as

SELECT

STATE

,sum(MEMBER_COUNT) as TOT_MBR_COUNT

,sum(MEMBER_SPEND) as TOT_MBR_SPEND

FROM RECORD1

GROUP BY STATE

order by STATE;

quit;

Accepted Solutions
Solution
‎11-07-2014 09:25 AM
Super Contributor
Posts: 578

## Re: Proc Sql: Is it possible to sum by group and also sum overall at the same time?

you could always just insert the total..

proc sql;

create table SUM_BY_STATE as

SELECT

STATE

,sum(MEMBER_COUNT) as TOT_MBR_COUNT

,sum(MEMBER_SPEND) as TOT_MBR_SPEND

FROM RECORD1

GROUP BY STATE

order by STATE;

insert into SUM_BY_STATE (state, mbr_count, mbr_spend)

select

'Grand Total' as State,

sum(mbr_count),

sum(mbr_spend)

from record1;

quit;

All Replies
Solution
‎11-07-2014 09:25 AM
Super Contributor
Posts: 578

## Re: Proc Sql: Is it possible to sum by group and also sum overall at the same time?

you could always just insert the total..

proc sql;

create table SUM_BY_STATE as

SELECT

STATE

,sum(MEMBER_COUNT) as TOT_MBR_COUNT

,sum(MEMBER_SPEND) as TOT_MBR_SPEND

FROM RECORD1

GROUP BY STATE

order by STATE;

insert into SUM_BY_STATE (state, mbr_count, mbr_spend)

select

'Grand Total' as State,

sum(mbr_count),

sum(mbr_spend)

from record1;

quit;

SAS Employee
Posts: 85

## Re: Proc Sql: Is it possible to sum by group and also sum overall at the same time?

You can also union the total at the bottom of the SQL:

proc sql;

create table SUM_BY_STATE as

SELECT

STATE

,sum(MEMBER_COUNT) as TOT_MBR_COUNT

,sum(MEMBER_SPEND) as TOT_MBR_SPEND

FROM RECORD1

GROUP BY STATE

union

SELECT

'Grand Total' as State,

,sum(MEMBER_COUNT) as TOT_MBR_COUNT

,sum(MEMBER_SPEND) as TOT_MBR_SPEND

FROM RECORD1

quit;

🔒 This topic is solved and locked.