SPSS Statistics

Expand all | Collapse all

change the length of all string variables with spss syntax or python

  • 1.  change the length of all string variables with spss syntax or python

    Posted Wed August 04, 2021 08:23 AM
    Dear all,

    I have a question: how can I change the length of all string variables with spss syntax or python?  so that their length 3 times smaller

    var1 var2 ... has length 48, 15, 120 e.g

    how can I change their formats to A16 A5 and A40? 

    thanks!

    xq

    ------------------------------
    xiaoqin Oezener-Wan
    ------------------------------


  • 2.  RE: change the length of all string variables with spss syntax or python

    Posted Wed August 04, 2021 08:48 AM
    Use the ALTER TYPE command.  Besides a fixed length, it also has the option of setting to the minimum length that does not lose any of the data.

    --





  • 3.  RE: change the length of all string variables with spss syntax or python

    Posted Wed August 04, 2021 09:09 AM
    Hello Jon, thanks for your reply!

    yes I could use alter type for every variable, but I have many variables, I tried to use python, but it didn't work 

    begin program python3.
    import spss
    for ind in range(spss.GetVariableCount()): 
    varNam = spss.GetVariableName(ind) 
    if spss.GetVariableType(ind) > 0:
     spss.Submit("Alter type (Aspss.GetVariableType(ind)/3)"
    end program.

    regards

    ------------------------------
    xiaoqin Oezener-Wan
    ------------------------------



  • 4.  RE: change the length of all string variables with spss syntax or python

    Posted Wed August 04, 2021 09:11 AM
    sorry missing the variable

    begin program python3.
    import spss
    for ind in range(spss.GetVariableCount()): 
    varNam = spss.GetVariableName(ind) 
    if spss.GetVariableType(ind) > 0:
     spss.Submit("Alter type varNam (Aspss.GetVariableType(ind)/3)"
    end program.

    the above lines don't work

    ------------------------------
    xiaoqin Oezener-Wan
    ------------------------------



  • 5.  RE: change the length of all string variables with spss syntax or python

    Posted Wed August 04, 2021 09:58 AM
    You can list all the variables in one ALTER TYPE command, including the use of TO.  You can also use  ALL (A=Asomething) to select all A-format variables or, e.g., (A20=A10) to select A20 variables.

    If that still isn't enough, you can use the SPSSINC SELECT VARIABLES extension command to define a macro based on patterns in the names and use that macro in ALTER TYPE.

    --





  • 6.  RE: change the length of all string variables with spss syntax or python

    Posted Wed August 04, 2021 10:14 AM
    One more approach.  Here is the Python equivalent.  Note the use of integer division.  Also, I used f strings for clarity in the syntax.  The drawback to this approach is that it will do a data pass for each string variable.

    begin program python3.
    import spss
    for ind in range(spss.GetVariableCount()): 
        varNam = spss.GetVariableName(ind) 
        if spss.GetVariableType(ind) > 0:
            newlen = spss.GetVariableType(ind)//3
            spss.Submit(f"Alter type {varNam} (A{newlen})")
    end program.




    --





  • 7.  RE: change the length of all string variables with spss syntax or python

    Posted Wed August 04, 2021 10:05 AM
    Hello Jon,

    I use these lines, they work! but I saw an error message in output 

    begin program python3.
    import spss
    for ind in range(spss.GetVariableCount()): #Loop through variable indices
    varNam = spss.GetVariableName(ind) #Look up each variable name
    if spss.GetVariableMeasurementLevel(ind) !='scale':
    t ="A" + str(int(int(spss.GetVariableFormat(ind)[1:])/3))
    spss.Submit("Alter Type " + varNam + "(" + t + ")")
    end program.


    Traceback (most recent call last):
    File "<string>", line 6, in <module>
    ValueError: invalid literal for int() with base 10: '8.2'

    but the format was actually changed as I wanted 

    I don't know what this error message meant

    ------------------------------
    xiaoqin Oezener-Wan
    ------------------------------



  • 8.  RE: change the length of all string variables with spss syntax or python

    Posted Wed August 04, 2021 10:16 AM
    You need to use integer division - //.  In Python3, / means floating point division regardless of the operand types.

    --





  • 9.  RE: change the length of all string variables with spss syntax or python

    Posted Wed August 04, 2021 10:20 AM
    many thanks, Jon! your syntax works perfect! :-)

    ------------------------------
    xiaoqin Oezener-Wan
    ------------------------------