```Date: Wed, 21 Apr 1999 22:34:56 GMT Reply-To: DNordlund Sender: "SAS(r) Discussion" From: DNordlund Organization: AOL http://www.aol.com Subject: Re: Using LAG Function to Decay a Variable over Time Jim Savage at ljas@earthlink.net wrote: > >I am trying to do a simple calculation in which I want to have the values of >a >new variable build up based on the values of an input variable and then decay >when the input variable declines. This is being used in Advertising research >where we calculate a decayed/smoothed measure based on TV Gross Rating Points >which are usually spikes or pulses. Here's the logic that I am trying to do: > >Decayed GRP's for week 1 = Input GRP's for week1 >Week 2 decayed GRP's = Input GRP's for week 2 + 80% of the Wk1 decayed GRP's > >Week 3 decayed GRP's = Input GRP's for week 3 + 80% of the Wk2 decayed GRP's > > >After these decay operations are done for each week the result is raised to >the >.65 power. > >This works really well in a spreadsheet, but I thought it would be easier in >SAS, since that's where I'm going to do the model anyway. > >Here's what I tried with no success. I think there is something I don't >understand about the LAG function. > >Data two; set one; > by market; >retain DECAYGRP; >DECAYGRP=GRP; >DECAYGRP=lag1(DECAYGRP)*.8); >DECAYGRP=SUM(of GRP DECAYGRP);); >if first.market then DECAYGRP=GRP; /* want to start over for each market */ >run; > >The first 2 observations are calculated correctly, but from then >on the values are wrong. Anyone have any thoughts as to >what I can do to fix this problem? > Jim, people often misunderstand how LAG works. But there is a simple solution. Try something like the following (test it for suitability): Data two; set one; by market; retain DECAYGRP; ***reset DECAYGRP here; if first.market then DECAYGRP=0; DECAYGRP=DECAYGRP*0.8+GRP; run; Hope this helps, Dan Nordlund ```

Back to: Top of message | Previous page | Main SAS-L page