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
------------------------------
Original Message:
Sent: Wed August 04, 2021 09:57 AM
From: Jon Peck
Subject: change the length of all string variables with spss syntax or python
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.
--
Original Message:
Sent: 8/4/2021 9:09:00 AM
From: xiaoqin Oezener-Wan
Subject: RE: change the length of all string variables with spss syntax or python
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
Original Message:
Sent: Wed August 04, 2021 08:47 AM
From: Jon Peck
Subject: change the length of all string variables with spss syntax or python
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.
--
Original Message:
Sent: 8/4/2021 8:23:00 AM
From: xiaoqin Oezener-Wan
Subject: change the length of all string variables with spss syntax or python
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
------------------------------
#SPSSStatistics