SPSS syntax - use path of the file

Deadly 提交于 2019-12-18 07:00:09

问题


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


回答1:


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:

<Command xmlns="http://xml.spss.com/extension" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="SET JOB CWD" Language="Python">
</Command>

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.




回答2:


In v21, the (free) Python plugin is a separate download. It is fully integrated in v22.

With the Python and R Essentials, you get a bunch of extension commands that work like native commands, including a dialog box interface. One you might find useful is STATS OPEN PROJECT. It lets you define a project or set of related projects and automatically load or execute ancillary files at startup or when you invoke the command.

In v21-22 you need to install this from the SPSS Community website. More details available if needed.

I would avoid the INCLUDE command, BTW. Use the newer INSERT command. It can do some directory tricks for you.



来源:https://stackoverflow.com/questions/28010865/spss-syntax-use-path-of-the-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!