如何将BEGIN PROGRAM-END PROGRAM结构放入宏中?

问题描述 投票:0回答:1

我为SPSS编写了Python脚本,我需要在语法上使用几次。因此,我想避免重复BEGIN PROGRAM-END PROGRAM.结构不止一次。我决定将其放在宏中,但遗憾的是,即使宏和脚本中都没有错误,它也无法正常工作。代码:

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...

当我运行此代码时,SPSS在clean宏的第一次调用处停止。如何简化我的代码并避免重复BEGIN PROGRAM结构?

spss
1个回答
0
投票

首先,在DEFINE-!ENDDEFINE.描述的底部明确指出,脚本代码不能放在宏https://www.ibm.com/support/knowledgecenter/pl/SSLVMB_25.0.0/statistics_reference_project_ddita/spss/base/syn_define_overview.html中。其次,您可以使用SCRIPT命令从外部文件运行脚本。您也可以将此命令放在宏中。关于BEGIN PROGRAM结构,有一个有趣的解决方法:

  1. 首先,您必须将带有BEGIN PROGRAM-END PROGRAM子句的Python代码复制到不同的新语法文件。假设此文件名为Python_cleaning.sps
  2. 第二,您需要用简单的BEGIN PROGRAM行替换clean宏内的INSERT结构。此命令允许从另一个运行外部语法。代码:
define clean ()

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

!enddefine.
  1. 最后,您可以使用clean宏以语法运行Python脚本。
© www.soinside.com 2019 - 2024. All rights reserved.