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.
End Result
By following the steps described in this tutorial, your final syntax file will look somewhat like the syntax below.
cd 'D:\employees'.
*2. Open data.
get file 'ori\employees.sav'.
*3. (Other commands).
*4. Save data.
save outfile '10_edited_data.sav'.
Final Notes
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.
THIS TUTORIAL HAS 15 COMMENTS:
By Katherine Baldock on November 23rd, 2015
Thanks, this is a great tutorial - excellent for beginners! In the first example of "get file" syntax, I think it should be a back slash, not forward slash (it's OK in the "end result" syntax example).
By Ruben Geert van den Berg on November 23rd, 2015
Thanks for your comment! When specifying paths in basic SPSS syntax, the forward or backward slash are interchangeable and we therefore use them interchangeably.
However, when using Python in SPSS, you should somehow escape backward slashes or prefix paths with an "r" as in
r'd:\project'
.By Rahul Kumar Sadhu on February 26th, 2016
Great stuff!
Lucid & helpful.
Thank you.
By Yotam Dalal on May 4th, 2016
Is there anyway to convert Syntax files to Data files in SPSS?
Thanks in advance,
Yotam
By Ruben Geert van den Berg on May 5th, 2016
Hi Yotam!
There kinda is: the DATA LIST command allows you to place your data in a syntax file. When you run it, the data values will show up in the data editor. Next, you can set up the dictionary as usual. I'll add a tiny example below.
Best,
Ruben
data list free/first last(2a20) age occupation(2f2).
begin data
"Ruben " "Tan" 25 3
"Chrissy" " van den Berg" 22 4
"Wijbe " "de Vries " 34 1
"Gerben " "Langeveld " 40 1
end data.
value labels occupation 1 'Contract' 3 'Self employed' 4 'Unemployed'.
variable labels age "Age in years".