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

Hi,

 

I have a data set with 100 records for customerID 1. I need for every row cumulative sum considering only 12 rows before the current row.

PS: In which case i will not have cumulative sum for the first 12 rows.

How can I do this in SAS?

Thanks in Advance.

 

IDNumbercum_sum
11 
12 
13 
14 
15 
16 
17 
18 
19 
110 
111 
112 
113 

 

So on........,

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

 

/* rolling 12-record sums*/

 

data want;

  set have;

  by id;

  counter+1;

  if first.id then counter=1;

  _sum+number - ifn(counter>12,lag12(number),0);

  if  counter>=12 then cum_sum=_sum;

  drop _sum;

run;

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

5 REPLIES 5
Reeza
Super User

If you have SAS ETS, using PROC EXPAND is the easiest method. Otherwise:

 

data have;
	ID=1;

	do number=1 to 13;
		output;
	end;
run;

data want;
	set have;
	by id;
	retain running_total counter;

	if first.id then
		do;
			cum_sum=0;
			counter=0;
		end;
	running_total+number;
	counter+1;

	if counter>12 then
		cum_sum=running_total;
run;

PROC EXPAND

 

proc expand data=have out=want;
by id;
convert number=cum_sum / transformout=(cusum 12 trimleft 12);
run;

 

 

Astounding
PROC Star

If I understand correctly, for each ID:

 

  • On observations 1-12, cum_sum should be missing
  • On observation 13, cum_sum should be the sum from observations 1-12
  • On observation 14, cum_sum should be the sum for observations 2-13

If that's the case, a DATA step can code it:

 

data want;

set have;

by ID;

if first.ID then do;

   counter=0;

   temp_sum=0;

end;

back12 = lag12(number);

counter + 1;

if counter > 12 then do;

   cum_sum = temp_sum;

   temp_sum = temp_sum - back12;

end;

temp_sum + number;

drop number temp_sum back12;

run;

 

It's untested code, and it gives me a headache just to look at it.  But it ought to work.  Good luck.

Kurt_Bremser
Super User
data want;
set have;
by id;
if first.id then counter = 1;
else counter + 1;
cum_sum = sum(
  number,lag(number),lag2(number),lag3(number),lag4(number),lag5(number),
  lag6(number),lag7(number),lag8(number),lag9(number),lag10(number),lag11(number)
);
if counter lt 12 then cum_sum = .;
drop counter;
run;

Ksharp
Super User
data have;
	ID=1;

	do number=1 to 13;
		output;
	end;
run;
data want;
 set have;
 array x{0:11} _temporary_;
 if _n_ le 12 then cum_sum=.;
  else cum_sum=sum(of x{*});
 x{mod(_n_,12)}=number;
run;
mkeintz
PROC Star

 

/* rolling 12-record sums*/

 

data want;

  set have;

  by id;

  counter+1;

  if first.id then counter=1;

  _sum+number - ifn(counter>12,lag12(number),0);

  if  counter>=12 then cum_sum=_sum;

  drop _sum;

run;

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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!

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