Date: Fri, 4 Nov 2005 12:01:51 -0500
Reply-To: diskin@alum.rpi.edu
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Dennis Diskin <ddiskin@GMAIL.COM>
Subject: Re: Create a Roll Over Sum
In-Reply-To: <1131122041.736046.246750@z14g2000cwz.googlegroups.com>
Content-Type: text/plain; charset=ISO-8859-1
Yi,
Something like this?
var2=sum(var1,lag1(var1),lag2(var1));
if _n_ lt 3 then var2=.;
If the number of lags may vary, you could use a macro like:
%macro sums(nlag=);
var2=sum(var1
%do i = 1 %to &nlag-1;
,lab&i(var1)
%end;
);
if _N_ lt &nlag then var2=.;
%mend sums;
HTH,
Dennis Diskin
On 11/4/05, YI <yi_zhu_web@hotmail.com> wrote:
>
> Can anyone guide me how to figure out the following problem? Having a
> certain variable of data, I want to create another variable that sums
> up previous certain number of rows (including the current row) at each
> line.
> For example:
> With data Var1, and starting at row Three (say everything 3 steps), I
> want to create a variable Var2 that sums up previous 2 consecutive rows
> and the current row of Var1.
> Like the following:
> Var1 Var2
> 1
> 2
> 3 6 <-- sum(1+2+3)
> 4 9 <-- sum(2+3+4)
> 5 12 ... ...
> 6 15
> 7 18
> 8 21
> 9 24 <--sum(7+8+9)
>
> Thanks!
>
|