SPSS Statistics

 View Only
Expand all | Collapse all

Changing variable labels automatically (scripted, syntax, python)

  • 1.  Changing variable labels automatically (scripted, syntax, python)

    Posted Mon December 28, 2020 01:31 PM

    Hi everyone,

    I have survey data files coming in, that hold very long variable labels. A variable label could look something like this:

    "[This is a question that has been asked?] and a whole lot of stuff trailing the question, none of which i need".

    What I'd like to do, is retain the question (which is always between square brackets), and drop the rest. I've been able to do this in a text editor (BBEdit), using Regular Expressions... but I'd like to script it, macro it, python it.. within SPSS. So I can include it in a workflow.

    Anyone ever managed to do something like this?

    Thanks in advance!

    Ernst Jan Hölscher



    ------------------------------
    Ernst Jan Hölscher / ernstjan@me.com / Research in Psychology
    ------------------------------

    #SPSSStatistics


  • 2.  RE: Changing variable labels automatically (scripted, syntax, python)

    IBM Champion
    Posted Mon December 28, 2020 01:59 PM
    Here is a little Python code that will change variable labels with text in [] to just be the bracket contents.

    If the other part of the label has any use,  you might consider adding it as a custom attribute.  That could be incorporated into this code.

    begin program python3.
    import spssaux, re
        
    vardict = spssaux.VariableDict()
    for v in vardict:
      so = re.search(r"\[(.*?)\]", v.VariableLabel)
      if so is not None:
          v.VariableLabel = so.group(1)
    end program.

    --