* 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:\SPSS Syntax Seminar\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 to 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 str5auto. autorecode str2 /into str2auto. exe. list gender sex1 sex str5 str5auto str5a str2 str2auto. * 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 age = diff/(60*60*24*365.25). compute age1 = xdate.year(diff) - 1582. compute age2 = xdate.year(edate) - xdate.year(dob). exe. list edate dob diff age age1 age2. 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:\SPSS Syntax Seminar\data.sav'. document I collected this data on January 16, 2003 and blah blah blah. display document. * document drop. file label SPSS Syntax Seminar data file. save outfile 'd:\SPSS Syntax Seminar\data1.sav'. sysfile info 'd:\SPSS Syntax Seminar\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:\SPSS Syntax Seminar\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:\SPSS Syntax Seminar\data.sav'. aggregate outfile 'd:\SPSS Syntax Seminar\new.sav' /break gender /aveq1 = mean(q1). get file 'd:\SPSS Syntax Seminar\new.sav'. list. get file 'd:\SPSS Syntax Seminar\data.sav'. aggregate outfile 'd:\SPSS Syntax Seminar\new1.sav' /break gender /aveq1 = mean(q1) /sumq1 = sum(q1) /miss3 = numiss(q3) /pin5 = pin(q5, 2, 4). get file 'd:\SPSS Syntax Seminar\new1.sav'. list. * 13. reshaping data. * reshapting wide to long. get file 'd:\SPSS Syntax Seminar\data.sav'. list q1 to q3 /cases from 1 to 10. varstocases /make q from q1 to q3 /index = number /id = id /drop num1 to year. list. * reshaping long to wide. get file 'd:\SPSS Syntax Seminar\long.sav'. list. sort cases by trial. casestovars /id = trial /drop out2. list. get file 'd:\SPSS Syntax Seminar\long.sav'. sort cases by trial. casestovars /id = trial /index = ivar. get file 'd:\SPSS Syntax Seminar\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:\SPSS Syntax Seminar\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. * 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.