* 1 opening data files.
get file "d:\data08.sav".

get data
 /type = xls
 /file = 'd:\data08.xls'.

get stata file = 'd:\data08.dta'.

get sas data = 'd:\data08.sas7bdat'.

* 2 saving an SPSS data file.
* save the data file just as it is, but with a new name.
save outfile "D:\mydata.sav". 
* save the data file with a new name and reorder the variables.
save outfile "D:\mydata.sav"
 /keep = age yrs_edu gender all.
* save the data file with a new name and drop some variables.
save outfile "D:\mydata.sav"
 /drop = str1 str2 str3.

* 3 creating numeric variables.
* compute new_variable = old_variable.
compute new_var = old_var.

compute newvar123 = 0.
if num1 = 20 newvar123 = 1.
if num1 ge 50 or num2 <= 70 newvar123 = 2.
if num1=96 and num2 = 96 or num2 = 36 newvar123 = 3.
if num1=20 and (num2 = 96 or num2 = 30) newvar123 = 4.
execute.
list num1 num2 old_var new_var newvar123.

compute mult_num1_num2 = num1*num2.
compute div_num1 = num1/6.56.
* click on the values of div_num1 in the data editor.
compute sum_num1_num2 = sum(num1,num2).
exe.
list mult_num1_num2 div_num1 sum_num1_num2
 /cases from 1 to 10.

compute degree = $sysmis.
execute.
if yrs_edu = 12 or yrs_edu = 13 degree = 1.
if yrs_edu gt 13 and yrs_edu lt 16 degree = 2.
if yrs_edu = 16 or yrs_edu = 17 degree = 3.
if yrs_edu = 18 degree = 4.
if yrs_edu ge 19 degree = 5.
exe.
list yrs_edu degree.

* 4 creating standardized variables.
descriptives num1
 /save.
desc num1 (num1z)
 /save.
desc q1 (q1z) q2 (q2z) q5 (q5z)
 /save.

* 5 creating string variables.
string string1 (A4).
string string2 to string4 (A5).
compute string1 = "a".
if newvar123 <= 2 string1 = "B".
if str1 = "c" or str1 = "f" string1 = "c".
exe.
list newvar123 str1 string1.

* 6 the keyword "to".
autorecode v1 to v2 /into w1 to w3.
rename variables (string1 to string4 = s1 to s4).
compute z = mean(q1 to q5).
exe.
list v1 to v2 w1 to w3 q1 to q5 z.

* 7 recoding variables (numeric and string).
if num1 = 55 num1_new = 30.
if num1 le 50 and gender = "f" num1_new = 35.
if num1 > 80 or gender = "m" num1_new = 36.
list num1 gender num1_new.

recode num1 (lowest thru 60 = 1) (85 thru highest = sysmis)(else = 2) into num1_newer.
list num1 num1_new num1_newer.

string str1a str2a (A5).
recode s1 ("a" = "D") ("b" = "B") ("c" = ' ')(else='x') into str1a.
recode str2 ("b" = "Z") ("a" = ' ')(else = copy)  into str2a.
exe.
list s1 str1a str2a.

alter type str2a (A8).
recode str2 ("b" = "Z") ("a" = ' ')(else = copy)  into str2a.
exe.
list s1 str1a str2a.

* 8 changing string variables into numeric variables.

recode gender ("f" = 1) ("m" = 0) into sex.
recode str3 (convert) into str3_num.
compute str3_num1 = number(str3, f8.0).
exe.
list gender sex str3 str3_num str3_num1.

autorecode gender /into sex1.
autorecode str2 /into str2auto.
autorecode str3 /into str3auto.
exe.
list gender sex1 sex str2 str2auto str3 str3_num str3_num1 str3auto.

* the two commands below work for SPSS versions 13 and higher.
autorecode str2 str3 /into str2auto2 str3auto2 /group.
autorecode str2 str3 /into str2auto3 str3auto3 /blank = missing.
exe.
list str2 str2auto str2auto2 str2auto3 str3 str3auto str3auto2 str3auto3.
display dictionary.

* 9 Changing numeric variables into string variables.

string stid1 stid2 stid3 (A3).
compute stid1 = string(tid1, f3.0).
compute stid2 = string(tid2, f3.0).
compute stid3 = string(tid3, f2.0).
exe.

string stid123 (A8).
* compute stid123 = concat(ltrim(rtrim(stid1)), rtrim(ltrim(stid2)), (ltrim(stid3))).
compute stid123 = concat(rtrim(stid1), rtrim(stid2), (ltrim(stid3))).
exe.
list stid1 stid2 stid3 stid123.

* 10 sorting variables.

sort variables by name.

sort variables by type.

sort variables by missing.

sort variables by name.

* 11 delete variables.

delete variables s2 s3 s4 sex1 znum1.

save outfile 'd:\data08_deleted.sav'
 /drop s2 s3 s4 sex1 znum1.

* 12 variable levels.

variable level q1 to q5 (ordinal)
 /sex (nominal).

* 13 documenting data.
sysfile info 'd:\data08.sav'.
document I collected these data on January 16, 2003 and
blah blah blah.
add document "This is my additional comment"
"to my original document command.  Note "
"that each addition line of text must be enclosed in quotes.".
display document.
* drop documents.
file label SPSS Syntax Seminar data file.
save outfile 'd:\data081.sav'.
sysfile info 'd:\data081.sav'.
variable labels str1 'answer to question 7'
str2 'answer to question 8'.
display labels.

value labels yrs_edu 1 "high school" 2 "some college" 3 "Bachelors" 
4 "Masters" 5 "Ph.D.".
value labels q1 to q3 q5 1 'strongly disagree' 2 'disagree' 3 'agree'. 
add value labels q5 4 'strongly agree' 5 'not applicable'.
freq var = q1 to q5.

save outfile 'd:\data082.sav'.
display dictionary.

* 14 missing data.

missing values q1 to q5 (-9).
missing values q3 (-7).
missing values q1 q2 (-9 -8).
missing values str1 ('x').

missing values q5 ().

compute y = q1+q2.
compute y1 = sum(q1, q2).
exe.
list q1 q2 y y1.

* 15 creating and using filters (subsetting data).
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.

* 16 pasting syntax.
GET
  FILE='D:\data08.sav'.

analyze - descriptives - explore.

EXAMINE
  VARIABLES=num1 BY gender
  /PLOT BOXPLOT STEMLEAF
  /COMPARE GROUP
  /STATISTICS DESCRIPTIVES
  /CINTERVAL 95
  /MISSING LISTWISE
  /NOTOTAL.

examine num1 by gender.

* 17 the SPSS syntax guide.

* 18 the keywords "by" and "with".
unianova num1 by newvar123.

unianova num1 by newvar123 with q1.

recode new_var (2 = 1) (4 = 3) ( 6 = 5) (else=copy) into cat_var.
crosstabs binary by cat_var.
logistic regression binary with num1 cat_var num1 by cat_var
/categorical = cat_var.

* 19 for more information.