【问题标题】:How to put BEGIN PROGRAM-END PROGRAM structure inside a macro?如何将 BEGIN PROGRAM-END PROGRAM 结构放入宏中?
【发布时间】:2020-04-04 20:47:57
【问题描述】:

我为 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】:

    首先,在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. 其次,您需要将clean 宏中的BEGIN PROGRAM 结构替换为简单的INSERT 行。此命令允许从另一个运行外部语法。代码:
    define clean ()
    
    insert file = "C:\path\Python_cleaning.sps" 
    syntax = interactive error = stop cd = no encoding = "utf8".
    
    !enddefine.
    
    1. 最后,您可以使用 clean 宏以您的语法运行 Python 脚本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多