Anda di halaman 1dari 12

get file "C:\Program Files\PSPP\Syntax Seminar\ver11\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.









sd
x x
z
i



8881 3980441868 5972473741 -1.3111191
61 . 27
2 . 56 20

z

0873 3821079319 2785222745 0.68091271
61 . 27
2 . 56 75

z











* 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.
list q1 q2 q3 total.


* 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.





file label PSPP Syntax Seminar data file.
save outfile 'C:\Program Files\PSPP\Syntax Seminar\ver11\data1.sav'.
sysfile info 'C:\Program Files\PSPP\Syntax Seminar\ver11\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 'C:\Program Files\PSPP\Syntax Seminar\ver11\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 'C:\Program Files\PSPP\Syntax Seminar\ver11\data.sav'.
aggregate outfile 'C:\Program Files\PSPP\Syntax Seminar\ver11\new.sav'
/break gender
/aveq1 = mean(q1).
get file 'C:\Program Files\PSPP\Syntax Seminar\ver11\new.sav'.
list.



get file 'C:\Program Files\PSPP\Syntax Seminar\ver11\data.sav'.
aggregate outfile 'C:\Program Files\PSPP\Syntax Seminar\ver11\new1.sav'
/break gender
/aveq1 = mean(q1)
/sumq1 = sum(q1)
/miss3 = numiss(q3)
/pin5 = pin(q5, 2, 4).
get file 'C:\Program Files\PSPP\Syntax Seminar\ver11\new1.sav'.
list.


* 13. reshaping data.
* reshapting wide to long.
get file 'C:\Program Files\PSPP\Syntax
Seminar\ver11\data.sav'.
list q1 to q3
/cases from 1 to 10.



varstocases
/make q from q1 to q3 pspp
/index = number
/id = id
/drop num1 to year.
list.









get file 'C:\Program Files\PSPP\Syntax Seminar\ver11\long.sav'.
list.



sort cases by trial.
casestovars pspp
/id = trial
/index = ivar.
/drop out2.
list.






* 15. pasting code.
* analyze - descriptives - explore.

EXAMINE
VARIABLES=num1 BY gender
/PLOT BOXPLOT
/COMPARE GROUP
/STATISTICS DESCRIPTIVES
/CINTERVAL 95
/MISSING LISTWISE
/NOTOTAL.


* 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.






When working with dates, a potentially useful system variable is $jdate. This variable gives the current
date as the number of days from October 14, 1582.
compute today = $jdate.
exe.

18. For more information
http://www.ats.ucla.edu/stat/seminars/
pspp

Anda mungkin juga menyukai