Search code examples
spss

spss how to get the get the Width of a variable


I would like to know if there is a way to output the width of each variable in SPSS. i.e the variable PUMFID as a width of 5

enter image description here

My goal (if possible) is to recreate a data list as such format

variable-label*       start-column-width*     start-column-width + width*

or exactly as it is in the screenshot

enter image description here

import spss
spss.StartDataStep()
datasetObj = spss.Dataset()
varObj = datasetObj.varlist["PUMFID"]
print("Name : " + varObj.name)
print("measurementLevel:  " + str(varObj.measurementLevel))
print("columnWidth : " + str(varObj.columnWidth))
print("missingValues : " + str(varObj.missingValues))
print("role : " + str(varObj.role))
print("type : " + str(varObj.type))
print("attributes : " + str(varObj.attributes))
print("alignment : " + str(varObj.alignment))
print("index : " + str(varObj.index))
spss.EndDataStep()

result:

Name : PUMFID 
measurementLevel:  SCALE 
columnWidth : 8 
missingValues : (-2, 99996.0, 99999.0, None) 
role : Input 
type : 0 
attributes : {} 
alignment : 1 
index : 0

Right now I don't understand why the column width of PUMFID is 8 in the output of the python program (syntax file). It's supposed to be 5 as in the Variable View in SPSS: column Width

Dir list of varObj

print(dir(varObj))
['Attribute', 'ValueLabel', '_Variable__attr', '_Variable__ds', '_Variable__vallbl', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__re 
pr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_delattributes', '_delvalueLabels', '_getalignment', '_getattributes', '_getcolumnWidth', '_getformat', '_getlabel', '_getmeasurementLevel', '_getmissingValues', '_getname' 
, '_getrole', '_gettype', '_getvalueLabels', '_setalignment', '_setattributes', '_setcolumnWidth', '_setformat', '_setlabel', '_setmeasurementLevel', '_setmissingValues', '_setname', '_setrole', '_settype', '_setvalueLabels', 'alignment', 'attributes', 'c 
olumnWidth', 'format', 'index', 'label', 'measurementLevel', 'missingValues', 'name', 'role', 'type', 'valueLabels']

Solution

  • varObj.columnWidtis actually the Columns attribute from Variable View, and it is only a visual attribute. You may want to look and varObj.format, and it will return f5.0:

    F - is for numeric variables; 5 - is total number of characters (including the decimal sign!, for numerical variables) 0 - number of decimal places

    You will then need to slice the f5.0 string to get only the 5, if that is what you need. As far as I know, there is no property/attribute/command to get the width, other than via the format.