A best practice in SPSS is to open and save all files with syntax. Like so, it can be easily seen which syntax was run on which data. One could use the syntax generated by for this but there's a much shorter and better option.
Disadvantages of the Default Syntax
- If you open and save several files, the total amount of syntax will be rather large. Especially if you write (rather than paste) your syntax, this may be a bit annoying even though you can copy-paste the folder specification
- If you move your project to a different folder, you'll need to correct all paths in order for them to be valid again
Shortening the Syntax
cd 'C:\Documents and Settings\Work\Projects 2012\December\Some Customer'.
*Open data file.
get file 'Survey data.sav'.
How Does it Work?
CD
command sets a default directory. Whenever you open or save a file, it will be done from/to this directory. In case you're not sure what your default directory is, run
SHOW DIRECTORY.
In subsequent commands, you only have to type the file name, which is technically a relative path. Especially when you open or save multiple files, you'll need less syntax. More importantly, if you move your project to a different folder, you'll need to adjust only a single line of syntax (the cd
command, that is). Especially when a project involves multiple syntax files, this may prove a major advantage, especially when combined with INSERT.Using Subfolders
Whenever you use relative rather than absolute paths, SPSS quietly prefixes them with the default directory. When you'd like to access a file in a subdirectory of the default directory, you can specify only the subdirectory and the file name.
For example, if your default directory is C:\project
, then GET FILE 'data\data_file.sav'.
will open data_file.sav
from C:\project\data
.
Final Notes
CD applies to all files such as
- data files (SPSS, Excel or any other format)
- SPSS output files
- SPSS syntax files when using INSERT
- SPSS chart templates (only in SPSS version 19 onwards)
except tablelooks (“SET TLOOK ...”). I find this very annoying and I don't see why this hasn't fixed ages ago...
Thanks for reading!
THIS TUTORIAL HAS 16 COMMENTS:
By Jeff Boggs on November 22nd, 2017
Lovely! Thank you for the quick response.
I know that long ago many DOS programs seem allergic to underscores in directory names, and that this "allergy" carried over into Windows for some time.
By Jeff Boggs on November 22nd, 2017
Hello Ruben,
Sorry I didn't ask this in my last message, but is there a maximum character length for directory names using the SPSS CD command?
That is, if I set CD to a an absolute directory path that is over 256 characters, will SPSS still access it?
Best regards,
Jeff
By Ruben Geert van den Berg on November 22nd, 2017
Hi Jeff!
I'm not sure about that. Just give it a shot.
By Jeff Boggs on December 14th, 2017
It is possible to set up a CD command that uses IF, THEN logic? I have three machines, and two have identical directory structures. I would look to set up a CD command that first searches in one place, and if it does not find a directory at that location, then it searches in a second location.
I would prefer to do this using SPSS syntax.
ps with regard to my last message, I have been using CD with a directory structure with ca 110 characters:
'C:\Users\jboggs\Dropbox\DOKUMENTEN\Work\Projects_n_Presentations\Active\ACTIVE_Collaborations\2016+_PEPiN\Analysis'.
By Ruben Geert van den Berg on December 14th, 2017
Hi Jeff!
You can check if a folder exists with Python. I think it's something like
if os.path.exists(r"d:\data"):
If it does, have Python throw a CD command withspss.Submit("cd 'mydir'.")
Python is also aware of the SPSS installation directory which could also tell you which CD to set.Or try and throw in 3 CD commands and hope SPSS ignores the ones that specify non existing directories but I'm not sure that'll work.