Marissa
Marissa

Reputation: 21

Use Python in SPSS to recode only certain variables using loops?

I'm trying to use Python in SPSS to recode observations by row. Each row is a person's responses to a survey. If the participant dropped out of the survey midway through, the data from that point is equal to (-5). I need to change all of the -5's to system missing but I want to preserve any -5's that occurred before the survey drop.

i.e. if the original data looked like this:

[1 2 4 -5 6 7 1 -5 -5 -5 -5 ]
[ 2 3 4 5 -5 1 -6 -5 -4 -5 -5 ]
[ 4 5 -5 -5 -5 -5 -5 -5 -5 -5 ]

it would be changed to this:

[1 2 4 -5 6 7 1 . . . .  ]
[2 3 4 5 -5 1 -6 -5 -4 . . ]
[4 5 . . . . . . . . ] 

where each row is a different person's responses to all of the questions.

Does anyone know how I might do this? I've tried to use the python plugin in SPSS to iterate through rows and find the indices but I keep running into issues. Thank you so much!!

Upvotes: 1

Views: 354

Answers (1)

Andy W
Andy W

Reputation: 5089

You can do this is in vanilla SPSS syntax with a loop. Below is one example way to do it.

*Making fake example data.
DATA LIST FREE /X1 TO X11 (11F1.0).
BEGIN DATA
 1  2  4 -5  6  7  1 -5 -5 -5 -5
 2  3  4  5 -5  1 -6 -5 -4 -5 -5
 4  5 -5 -5 -5 -5 -5 -5 -5 -5 -5
-5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5
END DATA.

*Using vector and loop to recode ends to system missing.
VECTOR X = X1 TO X11.
LOOP #i = 11 TO 1 BY -1.
  IF X(#i) = -5 X(#i) = $SYSMIS.
END LOOP IF NOT MISSING(X(#i)).
EXECUTE.

Upvotes: 1

Related Questions