* Note that you may need to modify the paths when opening a data set, depending on where you saved the data on your computer. get file 'd:\data.sav'. * 1. creating numeric variables and using functions. compute newvar1 = num1. compute newvar = 0. if (num1 = 20) newvar = 1. if (num1 ge 50 or num2 le 15) newvar = 2. if num1=96 and num2 = 96 newvar = 3. if num1 ge 90 newvar2 = 1. execute. list num1 num2 newvar newvar1 newvar2. compute newvar3 = num1*num2. compute newvar4 = num1/6.56. compute newvar5 = sum(num1,num2). exe. list newvar3 newvar4 newvar5. * 2. creating standardized variables using desc /save. descriptives num1 /save. desc num1 (num1z) /save. * 3. creating and using string variables. string string1 (A4). string string2 string3 string4 (A10). compute string1 = "a". if newvar2 = 1 string2 = "b". if newvar1 ge 50 and newvar ne 1 string3 = "No". exe. list newvar newvar1 string1 to string4. recode str1 ("a", "b","c" = "D"). string str2a str2b str3a (A6). recode str2 ("c" = "D") ("a" = ' ') into str2a. recode str2 ("c" = "D") ("a" = ' ') (else=copy) into str2b. recode str3 ("c" = "D") ("a" = ' ') (else = 'x') into str3a. exe. list str1 str2 str2a str2b str3 str3a. * 4. recoding. if num1 = 55 y = 30. if num1 le 50 and gender = "f" y = 35. list num1 gender y. recode num1 (lowest thru 60 = 1) (85 thru highest = sysmis) into y1. list num1 y1. * 5. recode with (convert) and autorecode. recode gender ("f" = 1) ("m" = 2) into sex. recode str5 (convert) into str5a. exe. list gender sex str5 str5a. autorecode gender /into sex1. autorecode str5 /into str5auto1. autorecode str2 /into str2auto1. * the two commands below work for SPSS versions 13 and higher. autorecode str2 str5 /into str2auto2 str5auto2 /group. autorecode str2 str5 /into str2auto3 str5auto3 /blank = missing. exe. list str5 str5a str5auto1 str2 str2auto1 str5auto2 str2auto2 str5auto3 str2auto3. display dictionary. * 6. count command. * It counts the number of occurances of a value across a list of variables. count total = q1 to q3 (3). exe. * 7. keyword "to". * when creating variables, the SPSS keyword "to" will create variables with consecutive numbering. When using "to" in syntax, it means positionally consecutive (all variables between the first listed and the last listed will be included). * NOTE: The parentheses in the rename command are necessary. autorecode v1 to v2 /into w1 to w3. rename variables (v1 to v2 = b1 to b3). compute z = mean(q1 to q5). exe. * 8. dates. * dates are stored as numbers; you can add and subtract them. compute diff = edate - dob. compute age1 = diff/(60*60*24*365.25). compute age2 = xdate.year(diff) - 1582. compute age3 = xdate.year(edate) - xdate.year(dob). exe. list edate dob diff age1 age2 age3. compute date = date.dmy(day,month,year). exe. list day month year date. * Now go to view variables and change the type of variable. * Now let's extract the day from our date. compute exday = xdate.mday(date). exe. list day exday date. * 9. documenting data. sysfile info 'd:\data.sav'. document I collected these data on January 16, 2003 and blah blah blah. display document. * document drop. file label SPSS Syntax Seminar data file. save outfile 'd:\data1.sav'. sysfile info 'd:\data1.sav'. variable labels str1 'answer to question 7' str2 'answer to question 8'. display labels. value labels q1 1 'strongly disagree' 2 'disagree' 3 'agree' 4 'strongly agree'. value labels q2 to q3 q5 1 'strongly disagree' 2 'disagree' 3 'agree' 4 'strongly agree'. freq var = q1 to q5. save outfile 'd:\data2.sav'. display dictionary. * 10. Missing data. missing values q1 to q5 (-9). exe. missing values q1 (-8). missing values q1 (-9 -8). missing values str1 ('x'). exe. compute y = q1+q2. compute y1 = sum(q1, q2). exe. list q1 q2 y y1. * 11. using/creating filters, select if. filter by fltr. desc num1 num2. filter off. * use all. desc num1 num2. temporary. select if (gender = "f" and q1 ge 2). list num1. list num1. sort cases by gender. split file by gender. desc num1 num2. split file off. desc num1 num2. * 12. aggregate command. get file 'd:\data.sav'. aggregate outfile 'd:\new.sav' /break gender /aveq1 = mean(q1). get file 'd:\new.sav'. list. get file 'd:\data.sav'. aggregate outfile 'd:\new1.sav' /break gender /aveq1 = mean(q1) /sumq1 = sum(q1) /miss3 = numiss(q3) /pin5 = pin(q5, 2, 4). get file 'd:\new1.sav'. list. * 13. reshaping data. * reshapting wide to long. get file 'd:\data.sav'. list q1 to q3 /cases from 1 to 10. varstocases /make q from q1 to q3 /index = number /id = new_id /drop num1 to year. list. * reshaping long to wide. get file 'd:\long.sav'. list. sort cases by trial. casestovars /id = trial /drop out2. list. get file 'd:\long.sav'. sort cases by trial. casestovars /id = trial /index = ivar. get file 'd:\data.sav'. * 14. with and by in ANOVA and regression. * with indicates that a continuous variable will follow. * by indicates that a categorical variable will follow. get file 'd:\data.sav'. unianova num1 by gender. unianova num1 by gender with q1. logistic regression binary with num1 by gender /categorical gender. regress /dependent num1 /method = enter num2 binary. * 15. pasting code. * point-and-click: analyze - descriptives - explore. EXAMINE VARIABLES=num1 BY gender /PLOT BOXPLOT STEMLEAF /COMPARE GROUP /STATISTICS DESCRIPTIVES /CINTERVAL 95 /MISSING LISTWISE /NOTOTAL. examine num1 by gender. * 16. Syntax guide. * 17. System variables. compute id = $casenum. exe. compute miss = $sysmis. compute miss1 = 1. if missing(q1) or missing(q3) miss1 = $sysmis. exe. list miss q1 q3 miss1. compute today = $jdate. exe.