Search code examples
syntaxspss

Assigning partner values for paired rows in SPSS


Imagine a dataset with pairs of participants ("Couple" 1-10), each of whom have a unique identifier ("Person" 1 or 2). Each of these individuals within a pair have a unique value on some variable, call it "Actor". I want to write a script that takes the "Actor" value for the individual and puts it into a new variable for the other individual in the pair called "Partner." In this way, each Person (row) would have a value for themselves ("Actor") and a value of their partner ("Partner").

The method that I've attempted involves restructuring:

SORT CASES BY Couple.
CASESTOVARS
  /ID=Couple
  /GROUPBY=VARIABLE.

COMPUTE Partner.1=Actor.2.
COMPUTE Partner.2=Actor.1.
EXECUTE.

VARSTOCASES
  /MAKE Person FROM Person.1 Person.2
  /MAKE Actor FROM Actor.1 Actor.2
  /MAKE Partner FROM Partner.1 Partner.2
  /INDEX=Index1(2) 
  /KEEP=Couple 
  /NULL=KEEP.

Now this works perfectly for the small, hypothetical dataset that I've created. However, I'd like the script to be able to handle more variables without the user having to manually type in more /MAKE commands.

Something like this?

for i in varlist[var=all]
do
VARSTOCASES
/MAKE i FROM i.1 i.2
/INDEX=Index1(2).

But that's not valid SPSS code. Anyone know how I might rig this up?

Thanks!


Solution

  • The following code should do the trick:

    SORT CASES BY couple (A) Person (A).
    IF (couple = LAG(couple)) partner = LAG(actor).
    
    SORT CASES BY couple (A) Person (D).
    IF (couple = LAG(couple)) partner = LAG(actor).
    

    Explanation: First you sort your data set in a way, that every actor is followed by it's partner. Then you can use the LAG-function to copy the actor's id (from the upper row) into the partner's (second row) partner variable. Since there is no Follower-like function in SPSS (I'm really missing it) you can't copy a value from the second row into the first one directly. You have to sort the person variable in a descending order first.