1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
* SyntaxForItemAnalysisV6.sps .

***This Item Analysis syntax follows the example from the SPSS White Paper on the same subject.
* Data file must contain consecutive numeric variables q1 To qN where N is the number of questions (other variables are ignored)
* The last case of the file must contain the key (the correct answer) to each question.

*Instructions:
* To use the syntax, you only need to modify the macro call (in the last line of this syntax) to provide the data file specifications and the number of questions.
* Run the entire syntax.

* Initial syntax by Ray 2003/01/27 .
* Syntax improved by Raynald Levesque 2007/10/13.
* Tested with SPSS 15.


* 2015/09/01 The line 'SELECT IF NOT id_key.' before calling !keys macro was replaced with 'SELECT IF id_key.'
  as otherwise macro incorrectly marked last but one case as key case. That misprint affected only key marking
  with asterisk, without affecting test scoring. The problem was noted by Blair Copeland, Senior Network Engineer
  from University of North Texas System. Thanks, Blair!
* A.B.

*//////////.
DEFINE !keys(nbq=!TOKENS(1))
/* This macro adds the * after the correct answer number */
WRITE OUTFILE='define labels.sps'
!DO !cnt=1 !TO !nbq /!QUOTE(!CONCAT('VALUE LABELS q',!cnt," "))!CONCAT(q,!cnt)' "'!CONCAT(q,!cnt)'*".'
!DOEND.
EXECUTE.
!ENDDEFINE.
*//////////.


*////////////////////////////.
DEFINE !item(fileName=!TOKENS(1) /nbQuestions = !TOKENS(1))

CD !fileName.
DATASET CLOSE ALL .
GET FILE = !fileName .
PRESERVE.
SET TVARS=LABELS /TNUMBERS=LABELS.
COMPUTE id_tmp = $CASENUM. 
SORT CASES BY id_tmp(D). 
COMPUTE id_key = ($CASENUM=1). 
DO REPEAT key=#1 TO !CONCAT('#',!nbQuestions)   / q=q1 TO !CONCAT('q',!nbQuestions). 
- DO IF $CASENUM=1.
-   COMPUTE key = q. 
- ELSE.
-   COMPUTE q = (q=key). 
- END IF.
END REPEAT.

SORT CASES BY id_tmp (A).
***Delete the record containing the key.
SELECT IF NOT id_key.

***Compute the score for each record. 
COMPUTE score = MEAN(q1 TO !CONCAT('q',!nbQuestions))*100. 

***Compute and append the ranking of each score. 
RANK VARIABLES = score (D) /NTILES (3) INTO group /PRINT=YES /TIES=MEAN .
FORMATS group (F8).
VALUE LABELS group 1 'high' 2 'middle' 3 'low'. 
SAVE OUTFILE='scored.sav'.

***run against the resulting scored data to produce the output seen in Figures 3-4.
AGGREGATE OUTFILE=* 
 /BREAK=group !DO !idx=1 !TO !nbQuestions
 /!CONCAT('q',!idx, ' "percentage right q',!idx,'"=MEAN(q',!idx,')' ) !DOEND.

STRING nnames(A3).
COMPUTE nnames = CONCAT('v',LTRIM(STRING(group,F8))).

FLIP VARIABLES=q1 TO !CONCAT('q',!nbQuestions) /NEWNAMES=nnames.
RENAME VARIABLES (case_lbl=qnum) (v1=high) (v2=middle) (v3=low).
COMPUTE index = high - low. 
FORMATS high TO index (F4.2).
SUMMARIZE
  /TABLES=qnum high middle low index
  /FORMAT=VALIDLIST NOCASENUM TOTAL
  /TITLE='Index of Discrimination'
  /MISSING=VARIABLE
  /CELLS=NONE.

***Compute - among other things - corrected point biserial correlations.
GET FILE= 'scored.sav'.
RELIABILITY
  /VARIABLES=q1 TO !CONCAT('q',!nbQuestions)
  /FORMAT=NOLABELS
  /SCALE(ALPHA)=ALL/MODEL=ALPHA 
  /STATISTICS=DESCRIPTIVE CORR 
  /SUMMARY=TOTAL MEANS.

*** Produce a table similar to that in figure 5.

***Item distracter analysis setup. 
GET FILE=!fileName .
COMPUTE nobreak=1.
ADD FILES FILE=* /BY=nobreak /LAST=id_key.
SELECT IF id_key.
FORMATS q1 TO !CONCAT('q',!nbQuestions)(F1).

!keys nbq=!nbQuestions.

GET FILE=!fileName .
COMPUTE nobreak=1.
ADD FILES FILE=* /BY=nobreak /LAST=id_key.
SELECT IF NOT id_key.
MATCH FILES/FILE=* 
  /FILE='scored.sav' 
  /RENAME (q1 TO !CONCAT('q',!nbQuestions)= d1 TO  !CONCAT('d',!nbQuestions))
  /DROP=d1 TO !CONCAT('d',!nbQuestions)). 

VARIABLE LABELS group 'Percentage answering distracter'. 
VALUE LABELS group 1 'high 1/3' 2 'middle 1/3' 3 'low 1/3'. 
FORMATS q1 TO  !CONCAT('q',!nbQuestions) (F8).
INSERT FILE='define labels.sps'.
CROSSTABS
  /TABLES=group BY q1 TO !CONCAT('q',!nbQuestions) 
  /FORMAT=AVALUE TABLES 
  /CELLS=ROW.

RESTORE.
!ENDDEFINE.
*////////////////////////////.

SET MPRINT=YES.

!item fileName="c:\temp\ItemAnalysisExercise.sav"  nbQuestions =10 .