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
Here, you're increasing the cumulative sales and then outputting. You need to output first:
data want;
set have;
output;
cumulative_sales + sales;
run;
Here, you're increasing the cumulative sales and then outputting. You need to output first:
data want;
set have;
output;
cumulative_sales + sales;
run;
And using "by week" is only necessary, if you have multiple observations for one week.
Hi,
options missing=0;
data want;
set have;
x=lag(sales);
cum_sales+x;
drop x;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
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!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.