Session 14: Practical – Transfer to SPSS Transfer to SPSS Before you can start to analyse your data, you will need to transfer it from the data entry software into a statistics package. In this session we show how to transfer data into SPSS. At the start of the session the facilitator will go through the PowerPoint presentation on Transferring Data into SPSS, which considers transfer from CSPro, transfer from Access and transfer from Excel. You then have the option of watching the video on exporting from CSPro – this video is one of a series of videos on using CSPro for data entry and management which are available on the SSC’s YouTube Channel – www.youtube.com/user/SSCbroadcast - we have included an .mp4 file of this video on your memory stick. Transfer from Access In this first part of the practical you will transfer some of the data from Access into SPSS. For this you will need the Access database file “Baseline.accdb” provided on your memory stick. The data is taken from the Household Baseline Survey Questionnaire we used in the questionnaire layout exercise in Session 1 and a copy of the questionnaire together with variable names is included in your course folder. In the database the data are divided into separate tables for each section of the questionnaire and there is also a separate table for the fertiliser level data which, you may remember, is at a different level with each household able to have several fertiliser level records. The primary key at the household level is the combination of site ID, block ID, village ID and household ID. Open SPSS and from the File menu choose Open Database followed by New Query Select MS Access Database as the ODBC Data source and click Next Click Browse and locate the database file Baseline.accdb – click OK Adding an Access ODBC driver At this point you may come up against a problem with the ODBC transfer. The image below shows a possible error message you might get. This somewhat cryptic message means that the correct ODBC driver is not installed. With Office 2007 Microsoft changed the format of Access files and the file extension changed from .mdb to .accdb. If you are seeing this error message it is likely that you only have the driver for the earlier format of database. There is a solution: Click on Add ODBC Data Source in the Database Wizard. On the User DSN tab click on Add Scroll down the set of available drivers and select Microsoft Access Driver (*.mdb, *.accdb) – if you do not find this in the list you will need to search on the Internet and install it separately. Click Finish Give a suitable Data Source Name – something like MS Access 2007 – and click OK You should now find your new data source in the list of ODBC Data Sources in the Database Wizard. 1 SURVEY TRAINING PACK – SESSION 14 AGA KHAN FOUNDATION SSC Select your new data source and browse to find the file Baseline.accdb. This time it should open correctly. Selecting the table The available tables are listed as shown below: When importing from Access you can import data from several tables at the same time but we would recommend transferring each table one by one and merging them later if needed. This keeps the data files more manageable. In a later practical you will learn ways of merging data in SPSS. We’ll start by transferring data from Section 0. Double-click on Section0 in the left hand panel – this will bring all the fields across to the right hand panel. We could select a subset of the fields to bring across but for this exercise we are going to import all the fields. Click Next On the next screen you can set criteria to select which records to transfer. You might, for example, only want to transfer records from a particular village. We are going to transfer all the data. Click Next to continue The next screen shows the variable names and data types. String or text variables will generally be transferred with a width of 255 so if you had any memo fields in Access these would be truncated. In our data the only text fields are SITEID and VILLID and we know that these are only 4 characters in length. Tick the checkbox to Minimize string widths based on observed values – click Next SPSS now shows the SQL statement it has generated to transfer your data. At this stage we recommend pasting the syntax so that you can save the syntax to a file. Choose the option to Paste it into the syntax editor for further modification – click Finish 2 SURVEY TRAINING P ACK –SESSION 14 AGA KHAN FOUNDATION SSC The following syntax will be pasted. Note the full-pathname of the database will be different for you and the dataset name may be different if you have already opened other data files in this session. We suggest changing the dataset name from “DataSet1” to “Section0” GET DATA /TYPE=ODBC /CONNECT='DSN=MS Access Database;DBQ=S:\Projects\Aga Khan\Survey Training Pack\Module 4\Basic '+ 'SPSS Skills\Baseline.accdb;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;' /SQL='SELECT SITEID, BLOCKID, VILLID, HHID, CONSENT, FLDCODE, FLDDAY, FLDMTH, FLDYEAR, '+ 'SUPCODE, SUPDAY, SUPMTH, SUPYEAR, DE1CODE, DE1DAY, DE1MTH, DE1YEAR, DE2CODE, DE2DAY, DE2MTH, '+ 'DE2YEAR FROM Section0' /ASSUMEDSTRWIDTH=255. CACHE. EXECUTE. ALTER TYPE ALL(A=AMIN). DATASET NAME DataSet1 WINDOW=FRONT. In the syntax window select all the code and click the Run button or choose Run->All Check that all the data has come across correctly and there are no errors in the output file. You may notice that the data contains dates (date of interview, date of data entry, etc.) but these are currently stored with day, month and year in separate variables. Because of the way dates are treated by different packages and to avoid any confusion with date formats we generally recommend having the date separated in this way particularly when transferring between packages. Now that we have the data in SPSS we can use SPSS commands to combine these separate values into a formatted date variable. Add the following commands to the end of your syntax file. * Create dates from the separate variables. COMPUTE FLDDATE=DATE.DMY(FLDDAY,FLDMTH,FLDYEAR). COMPUTE SUPDATE=DATE.DMY(SUPDAY,SUPMTH,SUPYEAR). COMPUTE DE1DATE=DATE.DMY(DE1DAY,DE1MTH,DE1YEAR). COMPUTE DE2DATE=DATE.DMY(DE2DAY,DE2MTH,DE2YEAR). FORMATS FLDDATE SUPDATE DE1DATE DE2DATE (date11). VARIABLE LABELS FLDDATE "Date of Interview". VARIABLE LABELS SUPDATE "Date of supervisor check". VARIABLE LABELS DE1DATE "Date of first data entry". VARIABLE LABELS DE2DATE "Date of second date entry". EXECUTE. There are some things you should note about SPSS syntax: 1. Each command starts on a new line. 2. A command can span several lines. 3. Every command must end with a full-stop. 4. SPSS is not case-sensitive. 5. A line starting with an asterisk (*) is treated as a comment. In the syntax above, we are creating 4 variables using the COMPUTE command. The syntax for this command is: COMPUTE <variable-name> = <value>. In this case the value is calculated using the function DATE.DMY which takes 3 parameters: - the day, the month and the year; and converts this to a DATE value. 3 SURVEY TRAINING P ACK –SESSION 14 AGA KHAN FOUNDATION SSC The FORMATS command will apply a specified format to one or more variables. In this case the format we are using is (date11) – the format must be in brackets – and we are applying this format to the 4 variables we have just created. These date fields will be shown in the format DD-MMM-YYYY – for example 06-Dec2010. The command VARIABLE LABELS assigns a label to a variable. The label must be enclosed between quotes – these could be double or single quotes. Take care if your label includes a quote or an apostrophe; this is possible to do but the label itself must then be enclose in double-quotes. The EXECUTE command carries out any pending transformations. This is always needed after one or more COMPUTE commands. Run this syntax and scroll to the right in the Data View window to see the variables that have been created. In Variable View you should notice the variable labels that we have created. Note that the COMPUTE command can be created through using Transform->Compute Variable; however there is no menu option for creating the VARIABLE LABELS command so this command must be given in the syntax or by typing directly in the variable view window. Now save the data file using File-> Save As and give the name as Section0.sav - .sav is the extension used by SPSS for data files. While in the dialog box click the Paste button to save the syntax. Go to the syntax window and save the syntax giving a suitable name – the extension for SPSS syntax files is .sps Now following a similar procedure transfer the fertiliser data from the fertiliser table in the database, again saving the syntax as you go. Add comments to your syntax to remind you what the commands are doing. Transfer from Excel For the second exercise in this practical you will transfer the same set of data but this time from Excel. For this we have provided an Excel workbook called Baseline.xlsx. The data from each section of the questionnaire are stored in separate sheets in the file. As mentioned in the presentation, we can import directly from Excel by changing the file type in the Open Data dialog. From the File menu choose Open followed by Data In the dialog box change the Files of type to Excel (*.xls, *.xlsx, *.xlsm) Then locate the file Baseline.xlsx and click Paste. On the next screen we need to select the sheet and the range of data to import. We want to import the data from Section0. Make sure the checkbox read variable names from the first row of data is ticked and select the Section0 worksheet. Click OK The following syntax is pasted: (again the pathname and dataset name will be different for you) GET DATA /TYPE=XLSX /FILE='S:\Projects\Aga Khan\Survey Training Pack\Module 4\Basic SPSS Skills\Baseline.xlsx' /SHEET=name 'Section0' /CELLRANGE=full /READNAMES=on /ASSUMEDSTRWIDTH=32767. EXECUTE. DATASET NAME DataSet4 WINDOW=FRONT. Transfer the fertiliser data from the Excel workbook in the same way. 4 SURVEY TRAINING P ACK –SESSION 14 AGA KHAN FOUNDATION SSC Transfer from CSPro For the final exercise in this practical we have exported the data from CSPro ready to read into SPSS. You will remember from the presentation that when transferring between CSPro and SPSS this is more of a “push” from CSPro rather than a “pull” into SPSS as is the case with Access and Excel. During the export, CSPro creates a text file containing the data and an SPSS syntax file containing instructions for reading the data. The files we have provided are called: Section0.sps, Section0.dat, Fertiliser.sps and Fertiliser.dat The syntax files assume that the data files are located in the folder C:\AgaKhan – if this is not the case then you will need to edit the syntax accordingly. Open the file Section0.sps in SPSS and edit the pathname to match the location of the data file. Save the file then browse through to make sure you understand all the commands. The DATA LIST command is for reading fixed format text data from an ASCII file; each variable is read from fixed columns in the file. The VARIABLE LABELS command we have already mentioned; note that there is a single command to label all the variables and after the first variable all other variables are preceded by a forward slash. We then have a MISSING VALUES command which is for setting codes to be treated as missing value codes. Finally there is a VALUE LABELS command for setting labels for numeric codes. As with the VARIABLE LABELS command several variables are being labelled in this one command and each variable after the first one has a forward slash in front of it. When several variables share the same set of numeric codes and labels, they can all be listed against the same set of codes. CSPro on its export just creates a separate list for each variable. For example in the fertiliser data there are 4 variables for crops with identical codes – the command in the syntax file could be shortened to: VALUE LABELS FERTTYPE 1 "Urea" 2 "NPK" 3 "DAP" 4 "CAN" 5 "Rock phosphate" 6 "Local mixture" 7 "Several types" /FERTCRP1 FERTCRP2 FERTCRP3 FERTCRP4 3 "Sorghum" 4 "Maize" 7 "Finger millet" 8 "Millet" 10 "Cassava" 11 "Sweet potato" 12 "Banana" 14 "Beans" 16 "Peanuts" 21 "Sugar cane" 31 "Kale" 36 "Tomatoes". Run the commands to transfer and label the data. Save the data file as something appropriate – e.g. Section0.sav – and paste the SAVE command to the syntax file. Do the same with the fertiliser data saving the data file as Fertiliser.sav. 5 SURVEY TRAINING P ACK –SESSION 14 AGA KHAN FOUNDATION SSC You should now have two fully-labelled data files and we will use these files plus data from Section 1 in the next section where we talk about merging data in SPSS. When transferring from Access or from Excel we would recommend adding commands to label the variables and codes (i.e. VARIABLE LABELS and VALUES LABELS commands) to your syntax files. 6 SURVEY TRAINING P ACK –SESSION 14
© Copyright 2026 Paperzz