Search code examples
labelspssprogrammatically-created

Change All Value Labels to Numerics in SPSS


I need to change all the value labels of all my variables in my spss file to be the value itself.

I first tried - Value Labels ALL. EXECUTE.

This removes the value labels, but also removes the value entirely. I need this to have a label of some sort as I am converting the file and when there is no values defined it turns the value into a numeric. Therefore, I need the all value labels changed into numbers so that each value's label is just the value - value = 1 then label = 1.

Any ideas to do this across all my variables??

Thanks in advance!!


Solution

  • Here is a solution to get you started:

    get file="C:\Program Files\IBM\SPSS\Statistics\23\Samples\English\Employee data.sav".
    begin program.
    import spss, spssaux, spssdata
    spss.Submit("set mprint on.")
    vd=spssaux.VariableDict(variableType ="numeric")
    for v in vd:
        allvalues = list(set(item[0] for item in spssdata.Spssdata(v.VariableName, names=False).fetchall()))
        if allvalues:
            cmd="value labels " + v.VariableName + "\n".join(["  %(i)s '%(i)s'" %locals() for i in allvalues if i <> None]) + "."
            spss.Submit(cmd)
    spss.Submit("set mprint off.")
    end program.
    

    You may want to read this to understand the behaviour of fetchall in reading date variables (or simply exclude date variables from having their values labelled also, if they cause no problems?)