4. Part II: Using Syntax
4.2. Using syntax to ... set your working directory
There’s a really neat command in SPSS (and most statistical programs have something similar) that allows you to define a directory where you are accessing and saving your files – a place like we’ve just recently set up where all our files are in the same place – called the working directory.
It’s good practice to define the location of your working directory at the beginning of your SPSS syntax file, and will save you a lot of time in the long run. You can do this using the cd command (cd stands for “change directory”). You can also check the working directory using the show directory command.
The key commands for defining and checking your working directory:
We are now going to define our own working directory, which will be the place you saved your SPSS files earlier. Now would be a perfect time to get out your USB stick, plug it in, and then open windows explorer to determine the file path for your working directory.
Task: Define your working directory
- In your syntax file, under the text identifying the file, who wrote it and when it was written, but before the get file command, use the cd command to define your working folder location, e.g.,:
cd "C:\Temp\SPSS Workshop".
Notice how soon after you type “cd”, it changes colour to blue? Cool, huh?! Here’s what your syntax file might now look like:
Now that we’ve set out working directory, we can modify our get file command to remove the file path (which is now set using the cd command). Now, whenever we open and/or save a file using this syntax, we won’t need to type the file path. Makes writing the syntax much easier!