0

I wrote a Python script for SPSS that I need to use a couple of times in a syntax. Thus, I would like to avoid repeating BEGIN PROGRAM-END PROGRAM. structure more than once. I decided to place it within a macro but sadly it doesn't work even though there are no bugs neither in a macro nor a script. Code:

define mac_export (!positional !tokens(1))

  OUTPUT EXPORT
  /CONTENTS EXPORT = visible  LAYERS = printsetting  
  MODELVIEWS = printsetting
  /XLSX  DOCUMENTFILE = "tables1.xlsx"
  OPERATION = createsheet
  sheet = !quote(!unquote(!1))
  LOCATION = lastcolumn NOTESCAPTIONS = no

!enddefine.

define clean ()

begin program.
import spss, SpssClient

SpssClient.StartClient()

OutputDoc = SpssClient.GetDesignatedOutputDoc()

OutputDoc.SelectAllText()
OutputDoc.Delete()

OutputDoc.SelectAllLogs()
OutputDoc.Delete()

OutputDoc.SelectAllNotes()
OutputDoc.Delete()

OutputDoc.SelectAllTitles()
OutputDoc.Delete()

OutputDoc.SelectAllWarnings()
OutputDoc.Delete()
end program.

!enddefine.

*** REPORT.
ctables
  /table
  (att1 + att2 + att3)
  [s][mean f1.1]
  /slabels position = column visible = no
  /titles title = "Attitudes".

clean.
mac_export "Attitudes".

ctables
  /mrsets countduplicates = no
  /table gender > $STATEMENTS [colpct.responses.count f40.0] by age
  /slabels position = column
  /titles title = "Statements".

clean.
mac_export "Statements".

* AND OTHER TABLES...

When I run this code SPSS stops at the first call of clean macro. What can I do to simplify my code and avoid repeating BEGIN PROGRAM structure?

Soren V. Raben
  • 179
  • 1
  • 14

1 Answers1

0

First of all it's explicitly said at the bottom of the DEFINE-!ENDDEFINE. description that scripts' codes cannot be put inside a macro: https://www.ibm.com/support/knowledgecenter/pl/SSLVMB_25.0.0/statistics_reference_project_ddita/spss/base/syn_define_overview.html. Second, you can use SCRIPT command to run a script from an external file. You can also place this command inside a macro. Concerning BEGIN PROGRAM structure there is an interesting workaround:

  1. First, you have to copy Python code with BEGIN PROGRAM-END PROGRAM clause to a different, new syntax file. Let's assume this file is named Python_cleaning.sps.
  2. Second, you need to replace BEGIN PROGRAM structure inside your clean macro with a simple INSERT line. This command allows to run an external syntax from another. Code:
define clean ()

insert file = "C:\path\Python_cleaning.sps" 
syntax = interactive error = stop cd = no encoding = "utf8".

!enddefine.
  1. Finally, you can run Python script in your syntax using clean macro.
Soren V. Raben
  • 179
  • 1
  • 14