BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
clancaster
Fluorite | Level 6

I modified data into a new data set to look like this;

ID Trans Sales

01 1 $135.00
01 1 $75.00
01 1 $140.00
01 2 $140.00
02 1 $220.00
03 1 $135.00
03 1 $125.00
03 2 $80.00

 

I have to sum the sales by the ID. I hit a wall and would appreciate any feedback as I am stuck on how to properly syntax summing sales by ID. Here is my code I have that attempts that:

 

data employee_sales;
merge datain.sales datain.parts;
by part_no;
if quantity = . then delete;
drop part_no price quantity;
sales= quantity*price;
total_sales= SUM(sales);
run;
proc sort;
by descending ID ;
run;
proc print data=employee_sales noobs;
format ID z2. sales dollar8.2 ;
title 'Employee Sales';
run;

and the output for that

ID trans sales total_sales
03 1 $135.00 135
03 1 $125.00 125
03 2 $80.00 80
02 1 $220.00 220
01 1 $135.00 135
01 1 $75.00 75
01 1 $140.00 140
01 2 $140.00 140

 

It just repeats itself, does anyone have any idea what I am doing wrong?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

If you want total sales by ID then cannot do it in a data step that sorted by PART_NO.

You can use RETAIN and SUM(total_sales,sales).  Or just use a sum STATEMENT.

data employee_sales_raw;
  merge datain.sales datain.parts;
  by part_no;
  if quantity = . then delete;
  drop part_no price quantity;
  sales= quantity*price;
run;
proc sort;
  by id descending;
run;
data employee_sales ;
  set employee_sales_raw ;
  by id descending ;
  total_sales + sales ;
  if first.id then total_sales=0;
run;

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20

what do you mean by Horizontal summation?

 

I am unable to understand your expected output 

 

I could see the sales dataset and where's the parts dataset

Reeza
Super User

Unfortunately you have the wrong approach. SUM will sum all values within a ROW, not in a column. You want PROC MEANS. 

 

Here's a quick example on how it works, the last one includes one that has a grouping variable. 

https://github.com/statgeek/SAS-Tutorials/blob/master/proc_means_basic.sas

 


@clancaster wrote:

I modified data into a new data set to look like this;

ID Trans Sales

01 1 $135.00
01 1 $75.00
01 1 $140.00
01 2 $140.00
02 1 $220.00
03 1 $135.00
03 1 $125.00
03 2 $80.00

 

I have to sum the sales by the ID. I hit a wall and would appreciate any feedback as I am stuck on how to properly syntax summing sales by ID. Here is my code I have that attempts that:

 

data employee_sales;
merge datain.sales datain.parts;
by part_no;
if quantity = . then delete;
drop part_no price quantity;
sales= quantity*price;
total_sales= SUM(sales);
run;
proc sort;
by descending ID ;
run;
proc print data=employee_sales noobs;
format ID z2. sales dollar8.2 ;
title 'Employee Sales';
run;

and the output for that

ID trans sales total_sales
03 1 $135.00 135
03 1 $125.00 125
03 2 $80.00 80
02 1 $220.00 220
01 1 $135.00 135
01 1 $75.00 75
01 1 $140.00 140
01 2 $140.00 140

 

It just repeats itself, does anyone have any idea what I am doing wrong?


 

clancaster
Fluorite | Level 6
Oh thanks for the insight
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

you have provided only 1 table as incoming data, and you are using a merge why?

Please provide better detail and code that represents the data samples you provide.

 

Tom
Super User Tom
Super User

If you want total sales by ID then cannot do it in a data step that sorted by PART_NO.

You can use RETAIN and SUM(total_sales,sales).  Or just use a sum STATEMENT.

data employee_sales_raw;
  merge datain.sales datain.parts;
  by part_no;
  if quantity = . then delete;
  drop part_no price quantity;
  sales= quantity*price;
run;
proc sort;
  by id descending;
run;
data employee_sales ;
  set employee_sales_raw ;
  by id descending ;
  total_sales + sales ;
  if first.id then total_sales=0;
run;
clancaster
Fluorite | Level 6

Thank you. This helps quite a bit.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 6 replies
  • 1951 views
  • 3 likes
  • 5 in conversation