3

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

horace_vr
  • 3,026
  • 6
  • 26
  • 48

2 Answers2

5

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.

Jignesh Sutar
  • 2,909
  • 10
  • 13
  • Thanks a lot for your reply, I get the general idea. Small problem though: it looks like I do not have Python Plug-in installed, which is required to run the code... :( – horace_vr Jan 18 '15 at 17:21
  • Which version of SPSS are you using? – Jignesh Sutar Jan 18 '15 at 17:23
  • I am using version 21; according to [IBM], it should be included with the installation media, but I do not have admin rights :) – horace_vr Jan 18 '15 at 17:34
  • At time of installation of SPSS, it may have been opted to not install python plug in. But it can resprotectivly be installed. I would suggest gaining permissions from IT to be able to do this so you can achieve SPSS tasks with much more ease and efficiency, like you are trying to do with relative paths. – Jignesh Sutar Jan 18 '15 at 17:37
  • Yes, I'll have to talk to admins about that. Anyway, thanks a lot! – horace_vr Jan 18 '15 at 17:38
1

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.

JKP
  • 5,419
  • 13
  • 5