Tamala Bradham
Tamala Bradham

Reputation: 31

Carry Forward Data for Each Case

In SPSS, how do I pull forward longitudinal data to blank cells for each id?

Example:

ID Time Gender
1  1    M
1  2    .
1  3    .
2  1    F
2  2    .
2  3    .
3  1    .
3  2    F
3  3    .

How do I get to this?

ID Time Gender Gender_Recoded
1  1    M      M
1  2    .      M
1  3    .      M
2  1    F      F
2  2    .      F
2  3    .      F
3  1    .      .
3  2    F      F
3  3    .      F

and then once I am able to carry forward, is there a command to do the reverse like in example 3, 1 so that F will pull up?

Thank you!!!

Upvotes: 3

Views: 226

Answers (2)

eli-k
eli-k

Reputation: 11360

The solution @user45392 suggested is perfect, but here is another nice one:

First we'll put the existing values in the first line of every ID, then for repeating ID's we copy gender value from the row above:

sort cases by ID gender (d). 
if ID=lag(ID) gender=lag(gender). 

Upvotes: 2

user45392
user45392

Reputation: 622

You can use the AGGREGATE function to do this. You specify which variable to group by setting /BREAK and then create your aggregated variables using Aggregate Functions.

In this case, FIRST() will apply the first non-missing value within each ID to all the rows/observations with that same ID. Since it takes the first non-missing value, it both "carries forward" and "carries backward" the Gender value associated with each ID (solving both your cases).

AGGREGATE
 /OUTFILE=* MODE=ADDVARIABLES
 /BREAK=ID
 /Gender_Recoded=FIRST(Gender) .

Upvotes: 2

Related Questions