4

Currently I save files to the absolute file path SAVE OUTFILE='my/path/to/file.sav'. This is not optimal, so I would like to save files to dynamic/relative file paths like SAVE OUTFILE='file.sav'.

So I need to set current directory, but this works as above as well CD 'my/path/to/' and then save. But I am wondering if SPSS can't set directory automatically when opening files? We are usually a lot of people working with same syntaxes and we will always have to change the absolute file paths.

Edit: As Jignesh Sutar have stated I can as well use python extension. So I thought I could use something simple like:

BEGIN PROGRAM.
import spss,spssaux, os, SpssClient
SpssClient.StartClient() 

path = SpssClient.GetCurrentDirectory()
print path
spss.Submit(r"""CD = '%s'.""" % (path))
SpssClient.StopClient()
END PROGRAM.

But above will actually just output the script and nothing else, however, another simple case would be:

BEGIN PROGRAM.
import spss
firstvar=spss.GetVariableName(0)
print firstvar
END PROGRAM.

And this is indeed working fine.

Draco Malfago
  • 303
  • 5
  • 18

2 Answers2

4

SPSS has a FILE HANDLE and CD command (as you point out also) that aid to try make these type of thing easier.

However I opt for a different approach that I have all my job setup to use, which if you use Python can implement also.

You can get the dynamic location of a (saved) syntax file using python like so:

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

I have posted a detailed solution to this in the past which you can find here and may find helpful in your scenario also.

Community
  • 1
  • 1
Jignesh Sutar
  • 2,909
  • 10
  • 13
  • Cool. If you use `INSERT` you can also have it change the path to the current directory, – Andy W Dec 23 '16 at 15:12
  • Not sure what you mean...? – Jignesh Sutar Dec 23 '16 at 15:17
  • So if you have a syntax file that has `GET FILE = mystuff.sav.`, call it "A.sps". If you use `INSERT FILE = "YourDir\A.sps" CD = YES.` then the get file command inserted will look in the same directory that the "A.sps" file is located. – Andy W Dec 23 '16 at 15:25
  • Ahh neat. Gotchya. So you only have to define the main root path once in your main syntax but not your sub-syntaxes. – Jignesh Sutar Dec 23 '16 at 15:28
  • Still, I will have to change one line of code with the current dir. I am just wondering why SPSS doesn't have anything like set current dir to file path of workbook or syntax. Anyhow, can I include a python script on opening of SPSS? if that is one option. – Draco Malfago Dec 27 '16 at 11:14
  • If you setup the `custom extension command` as I detailed in my previous response to this question then you simply have to start any syntax with `SET JOB CWD.` and you'll dynamically have the directory of where that syntax is saved. As easy as that. You can have scripts run at start of SPSS session but I can't see how that would be useful in this instance. – Jignesh Sutar Dec 27 '16 at 13:29
  • Ah ye, i understand, however, just following your example in your other answer didn't work.. – Draco Malfago Dec 28 '16 at 08:40
  • okey, it did actually work, but I keep getting errors then and then out of no reason.. – Draco Malfago Dec 28 '16 at 08:50
1

Another possibility is to use the STATS OPEN PROJECT extension command. This opens a project and carries out the actions it defines. It can open data files, run any syntax, etc. You can have a master project that does things you always want and subprojects for specific work. It can be set to do this on Statistics startup if you want.

STATS OPEN PROJECT can be installed from the Extensions menu in V24 or Utilities > Extension Commands in V22 or 23.

JKP
  • 5,419
  • 13
  • 5