Date: Fri, 2 Jun 2006 11:52:09 -0400
Reply-To: jshuang@AOL.COM
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: J S Huang <jshuang@AOL.COM>
Subject: Re: weighted lagged summation
In-Reply-To: <97803592DCA54B4E95DA5308AF7663C5077659CF@mercury.bus.msu.edu>
Content-Type: text/plain; charset="us-ascii"
Cui:
Tru the following code with output following.
data temp;
input obs time x;
datalines;
1 1 1
1 2 2
1 3 3
1 4 4
1 5 5
2 1 2
2 2 3
2 3 5
3 1 1
3 2 5
3 3 6
3 4 8
;
run;
proc sort data=temp;
by obs time;
run;
data Result;
retain SumX PrevTime;
set temp;
by obs;
if first.obs then do;
SumX=0;
PrevTime=time;
end;
SumX=SumX/2**(time-PrevTime)+x;
PrevTime=time;
drop PrevTime;
run;
proc print data=Result;
var obs time x SumX;
run;
***** OUTPUT *****
Products List 08:07 Thursday, June 1, 2006 3678
Obs obs time x SumX
1 1 1 1 1.0000
2 1 2 2 2.5000
3 1 3 3 4.2500
4 1 4 4 6.1250
5 1 5 5 8.0625
6 2 1 2 2.0000
7 2 2 3 4.0000
8 2 3 5 7.0000
9 3 1 1 1.0000
10 3 2 5 5.5000
11 3 3 6 8.7500
12 3 4 8 12.3750
J S Huang
-----Original Message-----
From: Cui, Shaojie <cui@BUS.MSU.EDU>
To: SAS-L@LISTSERV.UGA.EDU
Sent: Thu, 1 Jun 2006 22:03:48 -0400
Subject: weighted lagged summation
I am trying to do a weighted lagged summation, something like a koyck lag
structure. But I am completely lost on how to write the code. My data is like
this:
obs time x sumx
1 1 1
1 2 2
1 3 3
1 4 4
1 5 5
2 1 2
2 2 3
2 3 5
3 1 1
3 2 5
3 3 6
3 4 8
For each obs, at each time time t, do a weighted sum of all Xs before time
t(including t). for example, for obs1, time=4, sumx is a weighed sum of 1, 2, 3,
4. The weight is determined by how far back x is from t, the more far back the
less weight.
The formula I am planning to use for weight is: 1/power(2,t1-t2), where t1 is
the time for the current row, and t2 is the time for the previous rows to be
summed. For example, for obs1 and time =4, x =1,2, 3,4 are to be summed, and the
weight for x=4 is 1, which is calculated from 1/power(2,4-4); the weight for x=3
is 1/power(2,4-3)=0.5; the weight for x=2 is
1/power(2,4-2)=0.25; and the weight for x=1 is 1/power(2,4-1) =0.125.
I hope I make this clear. Thank you very much for the help.
Anna
|