Help using Base SAS procedures

how get grand total from sql

Reply
N/A
Posts: 0

how get grand total from sql

i have data
product amt1 amt2
A 10 15
A 20 30
B 15 22
A 5 10
B 8 9

proc sql ;
create table a as
select sum(amt1),sum(amt2)
from data
group by product;
I use sql to sum amt1 and amt2 by product
product amt1 amt2
A 35 55
B 23 31
total
but don't have the grand total line , how i get it from sql ?
total 58 (35+23) 86(55+31)
many thanks.
N/A
Posts: 0

Re: how get grand total from sql

Posted in reply to deleted_user
Is this u want...if so..take..

//create sum using your query but slight change as expr1 & 2
create table sumtable as
select sum(amt1)AS Expr1,sum(amt2) As Expr2
from data
group by product;

// expr1 & 2 sum up..

select sum(Expr1) ,sum( Expr2) from data;

any Issues..plz tell..
thanks & regards,
giri
N/A
Posts: 0

Re: how get grand total from sql

Posted in reply to deleted_user
thanks giri2help, it is not exactly i want

i want to have grand total with only one proc sql

proc sql
create table a as
select product,sum(amt1),sum(amt2)
from data group by product;
quit;

the result from above sql is only
output
A 35 55
B 23 32
but i want the below output from use one proc sql only.
data :
product amt1 amt2
A 10 15
A 20 30
B 15 22
A 5 10
B 8 9

output
A 35 55
B 23 32
total 58 87

thanks
Super User
Posts: 5,430

Re: how get grand total from sql

Posted in reply to deleted_user
It seems you want to build a report rather than a table. For that, there many other procs that is more suitable (summary, tabulate, report etc).
But, if you want to do it in one sql statement, you could use the above example, and linking the two queries together with a outer union corr:

create table sumtable as
select product, sum(amt1) as Expr1,sum(amt2) as Expr2
from data
group by product
outer union corr
select "Total" as product, sum(amt1) as Expr1, sum(Amt2) as Expr2
from data;

Whether this is one or two sql statements I think is just a matter of semantics.

/Linus
Data never sleeps
Ask a Question
Discussion stats
  • 3 replies
  • 196 views
  • 0 likes
  • 2 in conversation