Hi Everyone, Could someone explain to me what the LAG function does exactly? Joan Media Research Analyst Phone: +44 20 7593 1585 |
Hereβs what it says on pp111,112 of the syntax reference guide John F Hall LAG function LAG. LAG(variable[, n]). Numeric or string. The value of variable in the previous case or n cases before. The optional second argument, n, must be a positive integer; the default is 1. For example, prev4=LAG(gnp,4) returns the value of gnp for the fourth case before the current one. The first four cases have system-missing values for prev4. τ The result is of the same type (numeric or string) as the variable specified as the first argument. 112 Universals τ The first n cases for string variables are set to blanks. For example, if PREV2=LAG (LNAME,2) is specified, blanks will be assigned to the first two cases for PREV2. τ When LAG is used with commands that select cases (for example, SELECT IF and SAMPLE), LAG counts cases after case selection, even if specified before these commands. For more information, see the topic Command Order on p. 41. Note: In a series of transformation commands without any intervening EXECUTE commands or other commands that read the data, lag functions are calculated after all other transformations, regardless of command order. For example, COMPUTE lagvar=LAG(var1). COMPUTE var1=var1*2. and COMPUTE lagvar=LAG(var1). EXECUTE. COMPUTE var1=var1*2. yield very different results for the value of lagvar, since the former uses the transformed value of var1 while the latter uses the original value. From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of joan casellas Hi Everyone, Could someone explain to me what the LAG function does exactly? Joan Media Research Analyst Phone: +44 20 7593 1585 |
In reply to this post by joan casellas
Hi Joan,
Here is a syntax usage for Lag function. Sort cases by id. Compute flag=0. If (id=lag(id)) flag=1. Freq flag. This code will identify the id duplicates in your data file base on the flag variable. The id value is compared with the previous id value in the same column. Warmest Regards Dorraj Oet Date: Wed, 5 Oct 2011 09:12:36 +0100 From: [hidden email] Subject: Lag Function To: [hidden email] Hi Everyone,
Could someone explain to me what the LAG function does exactly?
Joan Media Research Analyst Phone: +44 20 7593 1585
|
something similar, on the Employee Data:
compute counter = 1.
sort cases by gender.
if ( gender eq lag(gender) ) counter = lag(counter) + 1.
exe.
This creates a variable that counts from 1 through N for both boys and girls.
Cheers!!
Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
In reply to this post by joan casellas
It refers to the record prior to the observation in question, or several before
depending on your specification. For example- - "if x=lag(x,1)" looks to see if x is equal to x in the prior observation. "if x=lag(x,2)" looks to see if x is equal to x in the observation 2 rows back. Quoting joan casellas <[hidden email]>: > Hi Everyone, > > > > Could someone explain to me what the LAG function does exactly? > > > > > > > > Joan > > Media Research Analyst > > Phone: +44 20 7593 1585 > > > > ---------------------------------------------------------------- This message was sent using IMP, the Internet Messaging Program. ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
Administrator
|
In reply to this post by joan casellas
I have nothing essential really to add beyond what others have stated (in particular John's quote of the FM). other than to _MOST_EMPHATICALLY_UNDERSCORE_
"When LAG is used with commands that select cases (for example, SELECT IF and SAMPLE), LAG counts cases **AFTER** case selection, **EVEN** if specified **BEFORE** these commands. Note: In a series of transformation commands without any intervening EXECUTE commands or other commands that read the data, lag functions are calculated *AFTER* ***ALL*** other transformations. SEE EXAMPLES..." If your data goes FUBAR after a LAG, check to see that you have an EXECUTE or some PROCEDURE (command that reads the data) prior to any selection or sampling. OTOH: I do **NOT** advocate the superstitious sprinkling of EXECUTE statements into production transformation code and when I see it madly exterminate the offending lines of code!
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
In reply to this post by Albert-Jan Roskam
Hi Joan, I think this is a personal obstacle that I had just passed today. I had this difficulty of identifying the latest purchase of the customer so that I can effectively point out which product to recommend next using my Sequence Detection model. Imagine you have this data.
The next thing to do is to sort by id in ascending order follow by date in descending order in which you will get this.
Once you have this, just use a duplicate check on the id column and select those flag with 0.
Warmest Regards Dorraj Oet Date: Wed, 5 Oct 2011 03:22:55 -0700 From: [hidden email] Subject: Re: Lag Function To: [hidden email] something similar, on the Employee Data:
compute counter = 1.
sort cases by gender.
if ( gender eq lag(gender) ) counter = lag(counter) + 1.
exe.
This creates a variable that counts from 1 through N for both boys and girls.
Cheers!!
Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
Free forum by Nabble | Edit this page |