Python in SPSS - KEEP variables
<p>I have selected the variables I need based on a string within the variable name. I'm not sure how to keep only these variables from my SPSS file. </p>

<pre><code>begin program.
import spss,spssaux
spssaux.OpenDataFile(r'XXXX.sav')
target_string = 'qb2'
variables = [var for var in spssaux.GetVariableNamesList() if target_string in var]
vars = spssaux.VariableDict().expand(variables)
nvars=len(vars)
for i in range(nvars):
    print vars[i]
spss.Submit(r"""
SAVE OUTFILE='XXXX_reduced.sav'.
ADD FILES FILE=* /KEEP \n %s.
""" %(vars))
end program.
</code></pre>

<p>The list of variables that it prints out is correct, but it's falling over trying to KEEP them. I'm guessing it's something to do with not activating a dataset or bringing in the file again as to why there's errors?</p>

<ol>
<li>You'll want to use the <code>ADD FILES FILE</code> command before the <code>SAVE</code> for your saved file to be the "reduced" file</li>
<li>I think your very last line in the python program should be trying to join the elements in the list <code>vars</code>. For example: <code>%( " ".join(vars) )</code> </li>
</ol>

python spss