LAG/LEAD functions

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

LAG/LEAD functions

Brock-15
Hi Everyone,

 

My dataset that I am working with is currently multi-record per ID.  Is
it possible to restart the lag/lead function for every ID or do
something that is equivalent?  I simply tried to modify the aggregate
command where I would get the first, last, of the variable of interest
restarted at every change in ID but I got an error.

 

Any help you can provide would be much appreciated!

 

Thanks in advance,

 

Brock

====================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/LEAD functions

Richard Ristow
At 11:03 AM 7/10/2008, Tibert, Brock wrote:

>My dataset that I am working with is currently multi-record per
>ID.  Is it possible to restart the lag/lead function for every ID or
>do something that is equivalent?

You mean, so it won't LAG across ID boundaries?

First, since there's no LEAD function as such, you may be using
CREATE. Although it doesn't seem to be documented, I understand that
CREATE does recognize SPLIT FILES.  So,

SPLIT FILES BY ID.
CREATE <whatever LAG, LEAD, etc., you have now>.

If you're using the LAG transformation function (as opposed to LAG in
CREATE), there are a variety of ways. Gene Maguin just posted a
solution, which is, roughly,

DO IF   ID EQ LAG(ID).
.  COMPUTE LagVarA = LAG(VarA).
.  COMPUTE LagVarB = LAG(VarB).
....
END IF.

There are some complications if either (a) the ID is composed of more
than one variable, or (b) you're using LAG for more than one record
back. Those are surmountable, but I'm not posting solutions unless
you need them. (I'm probably won't be able to post solutions in any
case; I'll be away, and entirely off-Net, for the next ten days.)

>I simply tried to modify the aggregate command where I would get the
>first, last, of the variable of interest restarted at every change
>in ID but I got an error.

That sounds like,

AGGREGATE OUTFILE=*
    /BREAK=ID
    /FirstVal = FIRST(VarOfInt)
    /LastVal  = LAST (VarOfInt).

So, that isn't it? If not, post a little input data, and the output
values you'd like to get from it.

-Best of luck,
  Richard

=====================
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