BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I have data
productcode and cost of each product, i group it by productcode i have total cost of each productcode but i don't have the amount of total cost of every product at the last line, could you help me how to do ?
productcode cost
A 100
A 20
B 200
B 50
C 220
....
my command
select productcode,sum(cost)
from data
group by productcode


I need the last line 490 (total of every product)
many thanks.
3 REPLIES 3
Olivier
Pyrite | Level 9
This is not really the SQL way of thinking to add grand total lines in a report or a data set. What you can do is same the result of your query in a SAS data set (CREATE TABLE ... AS SELECT etc.) and then display the dataset with a PRINT procedure, using the SUM statement to add the final row :
[pre]
PROC PRINT DATA = queryResult LABEL NOOBS ;
SUM cost ;
RUN ;
[/pre]
Regards.
Olivier
deleted_user
Not applicable
Hi Photo,
For accomplishing your task, the code posted by Mr.Olivier is enough but for creating a data set, the following code is helpful

data total;
set productcode;
by productcode;
if first.productcode then do;
total_cost+cost;
end;
if last.productcode;
run;
deleted_user
Not applicable
As oliver said proc is the best way.. U may use an ods to convert is back to data set..

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1222 views
  • 0 likes
  • 2 in conversation