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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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