neatdm路径_SPSS语法-文件的使用路径

I have a bunch of SPSS data and syntax files which I am moving around, changing folders on a daily basis. However, the relative paths remains the same.

Is there a way to make use of this fact?

e.g.: use the INCLUDE command and reference a syntax file which is always one path level up; or use GET to open a file, located two levels UP

Googling around I found some reference to the HOST command, but I did not quite make it to work.

Any input would be appreciated :)

Thanks a lot in advance

解决方案

You can get the relative path of a SPSS syntax (provided it is saved) using python.

SpssClient.GetDesignatedSyntaxDoc().GetDocumentPath()

From this you can then navigate to whichever folder you desire using pythons' os module (or otherwise). Below is an example of retrieving the saved file location of the syntax and then also the next two levels up. It also returns a macro containing the relevant folder paths stored as a strings so they can later be used in SPSS commands (such as GET, INCLUDE and others).

* Run this in any saved SPSS syntax to test *.

begin program.

import spss,spssaux,SpssClient, os

SpssClient.StartClient()

synPathL0U = os.path.dirname(SpssClient.GetDesignatedSyntaxDoc().GetDocumentPath())

SpssClient.StopClient()

synPathL1U=os.path.dirname(synPathL0U)

synPathL2U=os.path.dirname(synPathL1U)

print "synPathL0U =",synPathL0U

print "synPathL1U =",synPathL1U

print "synPathL2U =",synPathL2U

spss.SetMacroValue("!synPathL0U",spssaux._smartquote(synPathL0U+"\\"))

spss.SetMacroValue("!synPathL1U",spssaux._smartquote(synPathL1U+"\\"))

spss.SetMacroValue("!synPathL2U",spssaux._smartquote(synPathL2U+"\\"))

end program.

/* Check results - Echo should relay back the desired folder paths */.

echo !synPathL0U.

echo !synPathL1U.

echo !synPathL2U.

A neat way of implementing this to wrap it all up in a small custom extension command, so to avoid this boilerplate in all your syntaxes.

To do this it is easy as copying the code above between BEGIN PROGRAM / END PROGRAM into a function Run(args) to a python file called, say, SET_JOB_CWD.py. The name assigned to the file here is relevant and will be what is used later to call this extension command.

So SET_JOB_CWD.py would contain:

def Run(args):

import spss,spssaux,SpssClient, os

SpssClient.StartClient()

synPathL0U = os.path.dirname(SpssClient.GetDesignatedSyntaxDoc().GetDocumentPath())

SpssClient.StopClient()

synPathL1U=os.path.dirname(synPathL0U)

synPathL2U=os.path.dirname(synPathL1U)

spss.SetMacroValue("!synPathL0U",spssaux._smartquote(synPathL0U+"\\"))

spss.SetMacroValue("!synPathL1U",spssaux._smartquote(synPathL1U+"\\"))

spss.SetMacroValue("!synPathL2U",spssaux._smartquote(synPathL2U+"\\"))

Then also creating a corresponding SET_JOB_CWD.xml file containing the below code:

These two files should then be saved wherever your extension files are routed to (to know this folder location run SHOW EXTPATHS. in SPSS syntax, the location displayed for "EXTPATHS EXTENSIONS" is this folder.

Now, whenever you have a saved syntax in SPSS. You can simply run SET JOB CWD. and it will return the SPSS macros !synPathL0U,!synPathL1U,!synPathL2U containing the relevant folder locations stored as string.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值