SPSS Statistics

 View Only

 using insert file with python - skipped commands in inserted syntax file

Christoph Kulicke's profile image
Christoph Kulicke posted Wed September 03, 2025 07:48 AM

Hello everyone!

I have a syntax which should be executed for a number of files, which is why i used a python script to loop through a filename list.

However, when I open a file and execute the following command manually, everything works fine.

insert file = "02_Syntax\Clean_Data.sps".

But when I use python:

py_Clean = '''
"02_Syntax\\Clean_Data.sps".
EXECUTE.
'''
spss.Submit("py_Clean")

the Clean_Data syntax is being executed, but individual commands are missing. For example only some variables from a list of compute commands are created and only some variables from a list of alter type commands are being altered.

I already tried submitting the insert command individually as well as combined with the other python generated commands and also with and without the EXECUTE. The result remains the same.

I am at a complete loss as to why this may happen and would be grateful for any suggestions!

---

This is the complete syntax for context:

* Encoding: windows-1252.

cd "path\to\working\directory".

BEGIN PROGRAM.
import spss

filenames= ["file_1",
"file_2",
"file_3"]

for filename in filenames:
    py_getData = '''
    get data
    /TYPE = XLSX
    /FILE = "01_Data\\Original\\%s.xlsx"
    /SHEET=name 'Master' /CELLRANGE = FULL /READNAMES = ON /ASSUMEDSTRWIDTH = 32767.
    EXECUTE.
    ''' % (filename)
    
    py_cleanData = '''
    insert file = "02_Syntax\\Clean_Data.sps".
    EXECUTE.
    '''
    
    py_saveData = '''
    save outfile "01_Data\\%s.sav".
    EXECUTE.
    ''' % (filename)
    
    py_SyntaxParts = [py_getData , py_cleanData , py_saveData ]  
    spss.Submit(py_SyntaxParts)
END PROGRAM.
Jon Peck's profile image
Jon Peck IBM Champion

I would have to see more details to figure out the cause.  You might try submitting each syntax part as a separate Submit, which might clarify what is happening.  And double check that there are no error messages in the output.

BTW, there is no need to use the EXECUTE commands unless in INSERT files you have one of the rare cases where EXECUTE is needed, in which case the INSERT should be in those files..

Christoph Kulicke's profile image
Christoph Kulicke

What kind of context would be helpful to you?
Providing the dataset would be difficult, because I would need to remove all sensitive data. But I could do that, if it might help.

I tried to submit each syntax part individually, but that didn't change anything. The output contains no error messages, only the alter type results.

The Clean_Data.sps is fairly simple, but it might provide some insight:

COMPUTE sequence_number=$casenum.
COMPUTE datasource=2.
EXECUTE.

RECODE
var4 ('YES'='1') ('NO'='0') (else='9').
RECODE
var9 ('OptionA'='1') ('OptionB'='2') ('OptionC'='3') (else='9').
RECODE
varA to varZ (1 thru 5=copy) (else=9).
EXECUTE.

alter type Name (a1024).
alter type Number (f4).
alter type var1 (f11).
alter type var2 (a8).
alter type var3 (a20).
alter type var4 (f11).
alter type var5 (a12).
alter type var6 (a22).
alter type var7 (f11).
alter type var8 (a256).
alter type var9 (f1).
alter type var10 (a1164).
alter type varA to varZ (F11).

missing values var4 var9 varA to varZ (9).

Variable Labels
[...]

value labels
[...]

the two commands that are not executed, when the insert file command is submitted via python are COMPUTE sequence_number and alter type Name. 

Jon Peck's profile image
Jon Peck IBM Champion

Are you sure those commands are not executed?

What happens if you run the INSERT command directly?

Can you send me the Clean_Data file (jkpeck@gmail.com)?

Christoph Kulicke's profile image
Christoph Kulicke

I'm pretty sure. At least the alter type command does not show up in the output file and, when I look into the dataset, the sequence_number variable is not there and the Name variable has the original length.

When I execute the insert file command directly from the syntax editor after opening the dataset manually, the dataset contains both the sequence_number variable and the correctly typed Name variable.

I will pseudonymize the Clean_Data file and send it to you asap, after making sure, the issue still persists in that version.