Date: Tue, 5 May 2009 12:24:15 -0700
Reply-To: "Nordlund, Dan (DSHS/RDA)" <NordlDJ@DSHS.WA.GOV>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Nordlund, Dan (DSHS/RDA)" <NordlDJ@DSHS.WA.GOV>
Subject: Re: By group
Content-Type: text/plain; charset=windows-1252
> -----Original Message-----
> From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of
> SUBSCRIBE SAS-L Dan
> Sent: Tuesday, May 05, 2009 11:33 AM
> To: SAS-L@LISTSERV.UGA.EDU
> Subject: Re: By group
> HI. Richard:
> Thanks for jump in to answer part of my questions. However, I am still
> waiting my questions to be fully answered. I am assuming my role on this
> diagnose process, how could I get the values of "LAG Stores What". Because
> I could guess that the value of "LAG Returns What" to be the value of lag
> (score), I still could not figure out the value of "LAG Stores What". And
> it is important to know the value of "LAG Stores What" if we will be
> convinced by what Howards states.
> Thanks again.
This is the code that Howard was using the table to explain.
If not first.id then PrevScore = lag(score);
When the lag() function is used in a data step, a FIFO queue of length 1 is created and initialized with a value of missing at compile time. When the lag(score) function is executed, "LAG Returns What" is the value currently in the queue (this will be missing the very first time) and then the current value of score ("LAG Stores What") is pushed onto the queue. The next time lag(score) is executed, the value returned will be the current value in the queue (i.e. whatever was pushed onto the queue the last time lag(score) was executed). Then the current value of score is pushed onto the queue, overwriting what was there previously.
So, in line two of the table, lag() is executed and first returns the value currently in the queue ("LAG Returns What"), which is the missing value from the initialization process, and then the current value ("LAG Stores What") of score (=31) is pushed on to the queue. For obs 3, lag(score) returns the 31 that was previously stored there and then stores the current value of score (=29). Lag(score) always returns whatever is currently stored in the queue, and then stores the current value in the queue.
Here is a link to a previous post where I present a simulation of how lag() works. It may be useful in understanding what lag actually is doing.
Hope this is helpful,
Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204