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

Hello friends,

I have the following table sorted by Customer_ID and Transaction_Date:

Customer_ID, Transaction_Date, Store_Region, Amount

I would like to calculate running total such that each time a Customer or a Store_Region change, it starts over again.

Tried several solutions but all require sorting by Store_Region - this is not my case.

It is important that the data stays sorted by Customer_ID and Transation_Date.

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @shparber_m and welcome to the SAS Support Communities!

 

Have you tried using the LAG function?

data want;
set have;
by Customer_ID Transaction_Date;
if first.Customer_ID or Store_Region ne lag(Store_Region) then rtotal=Amount;
else rtotal+Amount;
run;

Or, alternatively, the NOTSORTED option of the BY statement?

data want;
set have;
by Customer_ID Store_Region notsorted;
if first.Store_Region then rtotal=Amount;
else rtotal+Amount;
run;

 

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

Sort by customer_id, store_region and transaction_date into a temporary dataset; calculate the running totals; then sort the result by customer_id and transaction_date.

shparber_m
Fluorite | Level 6

But I do not want to sort by Store_Region!

Could be a situation where a customer buys in NJ then in NY and the in NJ again.

I need each purchase to reset my running total in this case.

If I order by Store_Region as you propose, then I'll have NJ, NJ, NY and it will add two NJ into the second running total.

I dont want that. I want to order only by Customer_ID and Transaction_Date.

Please help.

Thank you!

ballardw
Super User

It may help to provide an example of what you have with several different customer_id, transaction_date, store_region and amount. Then show what you expect the result to be for that small example (small enough to do by hand).

 

 

shparber_m
Fluorite | Level 6

Sure! Here is a sample:
Each time that either customer_id or store_region change - I need to reset the running total.

The sort has to remain by Customer_ID and Transaction_Date only.

Please help.

Thank you!

Customer_IDTransaction_DateStore_RegionAmountRunning_Total
A2021-07-01NJ

100

100
A2021-07-02NJ100200
A2021-07-03TX100100
A2021-07-04NJ100100
A2021-07-05TX100100
A2021-07-06TX100200
B2021-07-01TX1010
B2021-07-02TX

10

20
B2021-07-03

NJ

1010
B2021-07-04TX1010
B2021-07-05NJ1010
B2021-07-06NJ1020

 

FreelanceReinh
Jade | Level 19

Hello @shparber_m and welcome to the SAS Support Communities!

 

Have you tried using the LAG function?

data want;
set have;
by Customer_ID Transaction_Date;
if first.Customer_ID or Store_Region ne lag(Store_Region) then rtotal=Amount;
else rtotal+Amount;
run;

Or, alternatively, the NOTSORTED option of the BY statement?

data want;
set have;
by Customer_ID Store_Region notsorted;
if first.Store_Region then rtotal=Amount;
else rtotal+Amount;
run;

 

shparber_m
Fluorite | Level 6

That's awesome!

Thanks a lot @FreelanceReinh 

Both your solutions work.

Thanks a lot!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 6 replies
  • 1032 views
  • 1 like
  • 4 in conversation