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

Hi,

I am trying to do a cumulative sum without much luck. I have tried joining the table to itself and the cumulative sum works, but the original dataset is very large and the query does not run .. So I am looking at alternatives and would appreciate any help from members.


I do not have access to proc expand 

The sold has negative values to reflect returns made on the day

The cumulative sum should group by store id, product code, sub code and days. I am looking at dataset where each store ID has several products and sub products for 365 days and all days have sold values (either 0, positive or negative).

Thanks in advance.

INPUT DATA

data have;

input storeid productcode$ productsubcode DAYS sold best12.;

datalines;

100 Y 1 1 2

100 Y 1 2 -1

100 Y 1 3 4

100 Y 2 1 2

100 Y 2 2 1

100 Y 2 3 0

101 Y 1 1 2

101 Y 1 2 -2

101 Y 1 3 2

101 K 4 1 2

101 K 4 2 1

101 K 4 3 1

;

run;

     

DESIRED OUTPUT

storeid productcode productsubcode DAYS sold cum_sold

100 Y   1 1 2 5

100 Y   1 2 -1 3

100 Y   1 3 4 4

100 Y   2 1 2 3

100 Y   2 2 1 1

100 Y   2 3 0 0

101 Y   1 1 2 2

101 Y   1 2 -2 0

101 Y   1 3 2 2

101 K   4 1 2 4

101 K   4 2 1 2

101 K   4 3 1 1

1 ACCEPTED SOLUTION

Accepted Solutions
Steelers_In_DC
Barite | Level 11

I think we are there:

data have;

input storeid productcode$ productsubcode DAYS sold best12.;

datalines;

100 Y 1 1 2

100 Y 1 2 -1

100 Y 1 3 4

100 Y 2 1 2

100 Y 2 2 1

100 Y 2 3 0

101 Y 1 1 2

101 Y 1 2 -2

101 Y 1 3 2

101 K 4 1 2

101 K 4 2 1

101 K 4 3 1

;

run;

proc sort data=have;by storeid productsubcode descending days;

data want;

set have;

by productsubcode notsorted;

sum + sold;

if first.productsubcode then sum = sold;

run;

proc sort data=want;by storeid productsubcode days;

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

I'm not following this example.

The first row of the raw data has a value for sold of 2. Shouldn't the corresponding cum_sold in the output data set also have a value of 2? Where does the cum_sold in your desired output come from?

--
Paige Miller
skillman
SAS Employee

This should give you close to what I think you want based on your description:

proc sql;

create table want as

select

  storeid,

  productcode,

  productsubcode,

  days,

  sum(sold) as cum_sold

from

  have

group by

  storeid,

  productcode,

  productsubcode,

  days;

quit;

Steelers_In_DC
Barite | Level 11

I also do not follow the example.  The output doesn't go along with what you are saying.  If you run this code you will not get the cum column that you have listed:

data have;

input storeid productcode$ productsubcode DAYS sold best12.;

datalines;

100 Y 1 1 2

100 Y 1 2 -1

100 Y 1 3 4

100 Y 2 1 2

100 Y 2 2 1

100 Y 2 3 0

101 Y 1 1 2

101 Y 1 2 -2

101 Y 1 3 2

101 K 4 1 2

101 K 4 2 1

101 K 4 3 1

;

run;

proc sql;

create table want as

select *,sum(sold) as sum_sold

from have

group by storeid,productcode,productsubcode,days;

Mumbai_1983
Calcite | Level 5

storeid productcode productsubcode DAYS sold cum_sold      calculation

100         Y                     1                 1            2         5           2+(-1)+4

100         Y                     1                 2           -1         3           -1+4

100         Y                     1                 3            4         4           4

This shows on day 1 -> 5 were sold and on day 2 -> 3 were sold and so on.

The cumulative totals should increment to day 1

Sorry, I just realized in the desired output the days has been left out.

Steelers_In_DC
Barite | Level 11

Here you go:

data have;

input storeid productcode$ productsubcode DAYS sold best12.;

datalines;

100 Y 1 1 2

100 Y 1 2 -1

100 Y 1 3 4

100 Y 2 1 2

100 Y 2 2 1

100 Y 2 3 0

101 Y 1 1 2

101 Y 1 2 -2

101 Y 1 3 2

101 K 4 1 2

101 K 4 2 1

101 K 4 3 1

;

run;

proc sql;

create table start1 as

select *,sum(sold) as _sum_sold2

from have

group by storeid,productcode,productsubcode

order by storeid,productsubcode,days desc;

data want;

set start1;

by storeid notsorted;

sum_sold + sold;

if days = 1 then sum_sold = _sum_sold2;

drop _:;

run;

proc sort data=want;by storeid productsubcode days;

Mumbai_1983
Calcite | Level 5

Thanks for your prompt response. The above query does not work beyond the first product code and sub code.  I have written the expected answer in brackets.

storeidproductcodeproductsubcodeDAYSsoldsum_sold
100Y1125
100Y12-13
100Y1344
100Y2123 (3 = 1 + 2)
100Y2216 (1 = 0 + 1)
100Y2305 (0 = 0)
101Y1122 (2)
101Y12-23 (0)
101Y1325 (2)
101K4124 (4)
101K4214 (2)
101K4313 (1)
Steelers_In_DC
Barite | Level 11

I think we are there:

data have;

input storeid productcode$ productsubcode DAYS sold best12.;

datalines;

100 Y 1 1 2

100 Y 1 2 -1

100 Y 1 3 4

100 Y 2 1 2

100 Y 2 2 1

100 Y 2 3 0

101 Y 1 1 2

101 Y 1 2 -2

101 Y 1 3 2

101 K 4 1 2

101 K 4 2 1

101 K 4 3 1

;

run;

proc sort data=have;by storeid productsubcode descending days;

data want;

set have;

by productsubcode notsorted;

sum + sold;

if first.productsubcode then sum = sold;

run;

proc sort data=want;by storeid productsubcode days;

Mumbai_1983
Calcite | Level 5

Thanks, that works perfectly well ... I think I understand what you have done .. By  going to the lowest level (product subcode), you have used the sum function at that level.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 8 replies
  • 1396 views
  • 0 likes
  • 4 in conversation