Lag Function

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Lag Function

joan casellas

Hi Everyone,

 

Could someone explain to me what the LAG function does exactly?

 

 

 

Joan

Media Research Analyst

Phone: +44 20 7593 1585                                     

 

Reply | Threaded
Open this post in threaded view
|

Re: Lag Function

John F Hall

Here’s what it says on pp111,112 of the syntax reference guide

 

 

John F Hall

 

[hidden email]

www.surveyresearch.weebly.com

 

 

 

 

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
Sent: 05 October 2011 10:13
To: [hidden email]
Subject: Lag Function

 

Hi Everyone,

 

Could someone explain to me what the LAG function does exactly?

 

 

 

Joan

Media Research Analyst

Phone: +44 20 7593 1585                                     

 

Reply | Threaded
Open this post in threaded view
|

Re: Lag Function

Jarrod Teo-2
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                                     

 

Reply | Threaded
Open this post in threaded view
|

Re: Lag Function

Albert-Jan Roskam
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?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: DorraJ Oet <[hidden email]>
To: [hidden email]
Sent: Wednesday, October 5, 2011 11:38 AM
Subject: Re: [SPSSX-L] Lag Function

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                                     
 


Reply | Threaded
Open this post in threaded view
|

Re: Lag Function

Linda Harrison
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
Reply | Threaded
Open this post in threaded view
|

Re: Lag Function

David Marso
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?"
Reply | Threaded
Open this post in threaded view
|

Re: Lag Function

Jarrod Teo-2
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.

 

id

date

product

1

10-Oct-96

1

1

10-Oct-98

2

1

10-Oct-07

3

2

10-Oct-96

1

2

10-Oct-98

2

 

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.

 

id

date

product

1

10-Oct-07

3

1

10-Oct-98

2

1

10-Oct-96

1

2

10-Oct-98

2

2

10-Oct-96

1

 

Once you have this, just use a duplicate check on the id column and select those flag with 0.

 

id

date

product

Flag

1

10-Oct-07

3

0

1

10-Oct-98

2

1

1

10-Oct-96

1

1

2

10-Oct-98

2

0

2

10-Oct-96

1

1

 

 
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?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: DorraJ Oet <[hidden email]>
To: [hidden email]
Sent: Wednesday, October 5, 2011 11:38 AM
Subject: Re: [SPSSX-L] Lag Function

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