SPSS – Opening Data with Syntax
There's several ways to open SPSS data files. The right way, however, is by syntax. The way we typically do so in practice may differ somewhat from what you're used to. But give it a shot. It keeps things nicely organized and this eventually saves time and effort. We'll use employees.sav throughout.
Creating a Project Folder
A previous tutorial recommended keeping your project files (that is, data, syntax, output and other files) together in a single folder. We first create this new folder and in it we'll create another folder which we call something like “original_data” or perhaps just “ori”. The screenshot below shows the result.
Copy Data File into Ori Folder
We'll now copy the original data file into the “ori” folder. Why? Well, it should be in our project folder rather than in an email or on a client's web server or something. This is because we want to make very sure the original data file -as we received it- gets backed up with the rest of our work. Second, putting it in a subfolder keeps it safely apart from the files we'll create and edit during the project.
Set SPSS Default Folder to Project Folder
Right, now we'll run SPSS and open a new syntax file. Next, we'll copy-paste the path to our project folder from its window into our syntax file. The result is shown below.
Next, we'll type a CD command around the path we just pasted. In our case, this results in cd 'D:\employees'. We run this line in order to set our project folder as SPSS’ default directory. If you're not sure whether that actually worked, running show dir. will tell you what's been set as SPSS’ default directory.
Open Data File
Right, now we'll move into the “ori” folder and copy the name of our data file. It can be selected by pressing f2 after left clicking it.
We paste the filename to the end of our syntax and prefix it with
ori/. Next, we'll type a
GET FILE command around it, resulting in
get file 'ori/employees.sav'.
Running this command will open the original data file from the “ori” subfolder.
Saving SPSS Data with Syntax
In practice, this is where you'll probably start inspecting, editing and analyzing your data. We'll skip that part, since it's not the focus of this tutorial. However, setting SPSS’ default directory makes it very easy to save your (edited) data to your project folder. You can do so by simply running something like save outfile '10_edited_data.sav'. Finally, save your syntax file into your project folder and you've all your project files nicely organized like we proposed in SPSS - Combining Data with Syntax and Output.
By following the steps described in this tutorial, your final syntax file will look somewhat like the syntax below.
*2. Open data.
get file 'ori\employees.sav'.
*3. (Other commands).
*4. Save data.
save outfile '10_edited_data.sav'.
The main point of this tutorial is that by using
CD, we need to include our project folder just once in our syntax. If we decide to move the entire folder or change its name, all we need to change is a single CD command at the top of our syntax file. This pays off by avoiding accidents and keeping things organized.