Search code examples
syntaxpathspss

SPSS syntax - use path of the file


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


Solution

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