1

I am working in SPSS and I need to create syntax to recode all numeric variables but only in case of a specific value of another value.

For all numeric variables I need to recode the value 50 to missing only in case that the variable status=0.

I need to find a way to do this automatically without listing all variables as I need a generic code that I can use for different datasets which is why I was thinking of using Python code within SPSS, but I have limited knowledge with that.

So it would need to be something like below, but then generic so that it selects automatically all numeric variables (excluding string variables)

DO IF (status=0).
 RECODE Q1 Q2 Q3 (50=sysmis)(else=copy).
END IF.
eli-k
  • 10,898
  • 11
  • 40
  • 44
La180923
  • 11
  • 1

3 Answers3

1

You can use spssinc select variables extention command to make a list of all your numeric variables and then use the list in regular syntax. So for every new dataset you can run this:

spssinc select variables macroname="!numericVars" /properties type = NUMERIC.
DO IF (status=0).
 RECODE !numericVars (50=sysmis)(else=copy).
END IF.
eli-k
  • 10,898
  • 11
  • 40
  • 44
0

This is a Python code snippet that demonstrates how you can achieve this in SPSS using the spss module:

import spss

# Specify the value you want to recode
old_value = 999

# Specify the new value to assign
new_value = 888

# Get a list of all numeric variables in the active dataset
numeric_vars = spss.GetVariableList().expand(spss.VariableType.NUMERIC)

# Loop through each numeric variable and recode the specific value
for var_name in numeric_vars:
    spss.Compute("{}.Recoded = {}.".format(var_name, new_value), var_name + " = {}".format(old_value))

# Commit the changes to the dataset
spss.Submit("DATASET SAVE.")

# Print a success message
print("Recode completed successfully.")
Hisham Alfoqaha
  • 239
  • 3
  • 11
0

Eli-k's answer is the most efficient answer and works like a charm out of the box, but since the question referenced python and to showcase how powerful the spss python integration can be, here is a custom class I wrote to do just that:

import spss
import spssaux

class Dataset: 

    def __init__(self): 
        self.varlist = spssaux.VariableDict().expand(spss.GetVariableName(0) + " to " + spss.GetVariableName(spss.GetVariableCount()-1))

    def getNumeric(self):
        nums = [v for v in self.varlist if spss.GetVariableType(self.varlist.index(v)) == 0]
        return nums 

    def recodeNumeric(self, code1, code2, if_clause = ''):
        nums = self.getNumeric()
        if if_clause == '':
            for num in nums:
                spss.Submit(f"recode {num} ({code1}={code2}).)")
        else:
            for num in nums:
                spss.Submit(fr'''do {if_clause}. 
    recode {num} ({code1}={code2}).
    end if.''')

basically, all you have to do now is instantiate the class with you dataset:

dta = Dataset()

and the call the recodeNumeric() method on the instance:

dta.recodeNumeric(50, "sysmis", "if status = 0")

be mindful though of passing your arguments correctly, numbers can remain int but sysmis for example needs to be passed as string and the if statement needs to be correct in the sense that it needs to conform to SPSS-syntax rules.

ragamuffin
  • 460
  • 2
  • 12