Hi everyone, I'm experiencing a problem when trying to create lagged values using a database in which data are nested/stacked. As can be seen from the database figure below (which serves as an example), I have 5 waves per participant, and there is 1 "mood" rating per wave (i.e., Level 1 unit). When I use the LAG function in SPSS, I'm able to create "lagged" values for mood ratings, but the problem is that the lagged values carry over across IDs. For instance, as you can see from the Figure, a lagged value was created for Wave #1 of ID#2, based on the last mood rating of ID#1. This is obviously a problem.... I was wondering whether there is an SPSS syntax/code that would allow to leave an empty cell for the first wave of each ID ? Or is there any way to create such a lagged variable without having values carrying over across IDs ? Thanks in advance for your help. O. |
One way is to condition the lag on the lag of the id being equal, e.g.
DO IF ID = LAG(ID). COMPUTE LagMood = LAG(Mood). END IF. EXECUTE. (It is best to execute lagged transformations before other sort/selection criteria occur.) Another way is to split the file by ID, and then use CREATE (or SHIFT VALUES) to make the lags. The SPLIT FILE will prevent the measures from crossing ID boundaries. SPLIT FILE BY ID. SHIFT VALUES Mood RESULT=LagMood LAG=1. *CREATE /LagMood=LAG(Mood 1). SPLIT FILE OFF. |
Administrator
|
I tend to use DO IF - END IF only when there are two or more computations that are conditional on some condition(s) being met. (That could be due to multiple COMPUTE lines between DO IF and END IF, or to the inclusion of one or more ELSE IF sections.) If there is only one conditional computation, I prefer a simple IF statement, like this:
IF ID = LAG(ID) LagMood = LAG(Mood). EXECUTE. /* As recommended by Andy. It accomplishes the same thing with fewer keystrokes.
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
In reply to this post by Oliver
sort cases by IDNO WAVE.
if IDNO = lag(IDNO) LAGX = lag(X). exe. On 22/07/2015 18:56, Oliver wrote: > Hi everyone, > > I'm experiencing a problem when trying to create lagged values using > a database in which data are nested/stacked. As can be seen from the > database figure below (which serves as an example), I have 5 waves per > participant, and there is 1 "mood" rating per wave (i.e., Level 1 unit). > When I use the LAG function in SPSS, I'm able to create "lagged" values for > mood ratings, but the problem is that the lagged values carry over across > IDs. For instance, as you can see from the Figure, a lagged value was > created for Wave #1 of ID#2, based on the last mood rating of ID#1. This is > obviously a problem.... > > I was wondering whether there is an SPSS syntax/code that would allow to > leave an empty cell for the first wave of each ID ? Or is there any way to > create such a lagged variable without having values carrying over across IDs > ? > > Thanks in advance for your help. > O. > <http://spssx-discussion.1045642.n5.nabble.com/file/n5730253/Lagged_values.png> > > > > -- > View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Lagged-variables-with-nested-stacked-data-tp5730253.html > Sent from the SPSSX Discussion mailing list archive at Nabble.com. > > ===================== > 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 -- -- Dr Chris Stride, C. Stat, Statistician, Institute of Work Psychology, University of Sheffield Telephone: 0114 2223262 Fax: 0114 2727206 "Figure It Out" Statistical Consultancy and Training Service for Social Scientists Visit www.figureitout.org.uk for details of my consultancy services, and forthcoming training courses, which are also available on an in-house basis: - Data management using SPSS syntax - Advanced SPSS syntax and SPSS macros - Testing for Mediation and Moderation using SPSS - Multi-level Modelling using SPSS - Introduction to Structural Equation Modelling using Mplus - Testing for Mediation and Moderation using Mplus - Multi-level Modelling using Mplus - Latent Growth Curve Modelling using Mplus ===================== 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 |
In reply to this post by Andy W
Andy, Bruce & Chris, Just wanted to say thank you for suggesting some codes in order to create lagged values in SPSS. I've tried all the proposed syntaxes with my database and everything worked great ! The lagged values do not cross ID boundaries in my stacked database, which is what I needed. Thanks again to all of you. O. |
Free forum by Nabble | Edit this page |