1.0 SPSS commands in this unit
|get data||used to read in data in different formats, such as Excel and comma separated values (.csv)|
|data list||used to read in data that are in fixed or list format|
|list||lists the data in the output window|
|get file||used to read in data that are in an SPSS data file|
2.0 Demonstration and explanation
In this unit we will show several ways of getting data into SPSS.
SPSS syntax needs to be put into an SPSS syntax file. To open a new SPSS syntax file, click on File, New, Syntax. You can either type in the syntax in the examples below, or you can copy and paste the syntax from each example into the syntax file. To run syntax, you can 1) highlight the syntax and click on the green arrow; 2) highlight the syntax and press CTRL-R on your keyboard (press the control key and the letter "R" at the same time); or 3) click on Run and then select All or Selection.
Here are a few things to keep in mind:
- There are three types of SPSS files that we will use during this class: data files, which end in .sav; syntax files, which end in .sps; and output files, which end in .spv. (Older SPSS output files end in .spo and need a special reader to be opened in newer versions of SPSS.)
- A syntax file is just a text file; you can open it in programs such as NotePad or WordPad.
- Comments start either with an asterisk (*) or the command comment.
- All commands end in a period (.).
- SPSS is not case-sensitive.
- You can have multiple data sets open in SPSS at once. Your syntax will run on the active data set.
2.1 Reading in an Excel file
We will start with inputting an Excel file. The variable names are on the first line of the Excel file.
* read in an excel (.xls) file. get data /type=xls /file = 'c:spss_datahs0.xls' /sheet=name 'hs0' /readnames=on.
2.2 Reading in a comma-separated-values text file
A comma-separated-values (.csv) format data file is a text data file and can be read in as follows. The variable names are on the first line of our data file hs0.csv. The same procedure will also work with tab-delimited data files.
* read in a text file. get data /type = txt /file = 'c:spss_datahs0.csv' /delimiters = "," /firstcase = 2 /variables = gender f1.0 id f3.0 race f1.0 ses f1.0 schtype f1.0 prog a10 read f2.0 write f2.0 math f2.0 science f2.0 socst f2.0. execute.
2.3 Reading in an ASCII fixed-format text file
The other type of commonly used ASCII data format is fixed format. It always requires a codebook to specify which column corresponds to which variable. Here is small example of this type of data with a codebook. One way of inputting this type of data is through the syntax editor.
195 094951 26386161941 38780081841 479700 870 56878163690 66487182960 786 069 0 88194193921 98979090781 107868180801
variable name column number id 1-2 a1 3-4 t1 5-6 gender 7 a2 8-9 t2 10-11 tgender 12
data list fixed file="c:spss_dataschdat.fix" / id 1-2 a1 3-4 t1 5-6 gender 7 a2 8-9 t2 10-11 tgender 12. list /cases = from 1 to 10.
2.4 Inputting data via the syntax editor
Using syntax editor, we can also input data directly. You can copy and paste the syntax below to the syntax editor and run it. Notice the difference in syntax between a numeric variable and a character variable.
data list list /id female race ses * schtype (A3) prog read write math science socst. begin data. 147 1 1 3 pub 1 47 62 53 53 61 108 0 1 2 pub 2 34 33 41 36 36 18 0 3 2 pub 3 50 33 49 44 36 153 0 1 2 pub 3 39 31 40 39 51 50 0 2 2 pub 2 50 59 42 53 61 51 1 2 1 pub 2 42 36 42 31 39 102 0 1 1 pub 1 52 41 51 53 56 57 1 1 2 pub 1 71 65 72 66 56 160 1 1 2 pub 1 55 65 55 50 61 136 0 1 2 pub 1 65 59 70 63 51 end data.
2.5 Opening an SPSS data file
Last but not least, here is how to open an SPSS data file.
* how to open an SPSS data file. get file 'c:spss_datahs0.sav'.
If you are using syntax, you will want to have the method of opening the data at the top of your syntax file. Please note that SPSS can have multiple data files open at once, and your syntax will operate on the active data set.
3.0 For more information
- SPSS Programming and Data Management, Fourth Edition
- Chapter 3
- SPSS Learning Modules
Inputting raw data into SPSS
Inputting data directly into the SPSS Data Editor
Reading dates into SPSS and using date variables