```Date: Wed, 21 Apr 1999 21:26:37 GMT Reply-To: FABIEN LABERENNE Sender: "SAS(r) Discussion" From: FABIEN LABERENNE Subject: Re: Using LAG Function to Decay a Variable over Time Jim Savage a icrit dans le message <37202bab.22419191@news.earthlink.net>... >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 Savage at ljas@earthlink.net > >Savage Genealogy: http://home.earthlink.net/~ljas/savage/index.htm >Landers Genealogy: http://home.earthlink.net/~ljas/index.htm > > Hi , I don't know if i understood exactly your problem but try replacing thess lines >retain DECAYGRP; >DECAYGRP=GRP; >DECAYGRP=lag1(DECAYGRP)*.8); >DECAYGRP=SUM(of GRP DECAYGRP);); >if first.market then DECAYGRP=GRP; by these if first.market then DECAYGRP=GRP; else DECAYGRP=lag(DECAYGRP)*0.8+GRP; According to me, you don't have to use the retain statement as its sounds to perform the same work as lag function I don't really know the function "SUM(of " that you use (I always perform proc means); Good luck, Regards, Fabien ```

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