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

Week Sales Cum_Sales
1        160       0
2        390      160
3        800      550
4        995     1350
5       1250    2345
6       1630    3595
7       1750
8       2000
9       2250
10     2500

 

The columns week and sales are provided in the dataset. I am creating a new column called cumulative_sales where the first entry is always zero and the consecutive entries are the sum of sales one week before that

 

data B2;
set B2;
by week;
if first.week then cumulative_sales=0;
cumulative_sales = cumulative_sales + sales;
if last.week then output;
run;

 

Please mention the corrections in the code

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Here, you're increasing the cumulative sales and then outputting.  You need to output first:

 

data want;

set have;

output;

cumulative_sales + sales;

run;

View solution in original post

3 REPLIES 3
Astounding
PROC Star

Here, you're increasing the cumulative sales and then outputting.  You need to output first:

 

data want;

set have;

output;

cumulative_sales + sales;

run;

andreas_lds
Jade | Level 19

And using "by week" is only necessary, if you have multiple observations for one week.

srinath3111
Quartz | Level 8

Hi,

 

options missing=0;
data want;
set have;
x=lag(sales);
cum_sales+x;
drop x;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

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
  • 9829 views
  • 1 like
  • 4 in conversation