Search code examples
syntaxspss

Using Write command to create new syntax - problem with unwanted spaces


I am trying to run a command on a list of variables stored as values in a different file. To do that I am creating a new syntax based on the variable names, like this:

WRITE OUT="\Selection.sps" 
    /"VARIABLE ATTRIBUTE VARIABLES = Final_",Var1," ATTRIBUTE=selectVars('yes')." .
EXECUTE.

The Problem is between Final and Var1, I am getting 11 spaces. The file in which I want to use this macro has variable names as Final_Var1 (So in the new file, Final is added to each variable's Name). How can I remove the space so that the new variable can be referred to properly? Should I create a new file or COMPUTE and CONCAT commands?


Solution

  • The write command is limited like that - you can't avoid the spaces or use trim. For commands like the one you're working on there is no way to build the command within the write command - you have to build the text in advance and then put it in the write command, so -

    strimg mycmd(a100).
    compute mycmd=concat("VARIABLE ATTRIBUTE VARIABLES = Final_", 
          ltrim(string(Var1,f4)), 
          " ATTRIBUTE=selectVars('yes').").
    WRITE OUT="\Selection.sps" /mycmd .
    exe.
    

    Note that this is not the only way to work on variable lists - you can use Python code within the syntax to build your variable lists more efficiently.