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
* Question was: How can I find random pairs of cases for T-Test?
* Note that the data file is NOT supplied with the code. 

* Current assumptions.
* control variables (eg altgrp1) have integer values between 0 and 9.
* sample variables (eg labfibg) have values of 1 or another integer.
* maximum number of characters in name of the control variables is 7.
* (the above condition is required because a 1 is added at the end of these names).
* there are less than 100,000 cases being studied.

* The above restrictions could be removed by making the macro more general.
* Raynald Levesque Raynald@spsstools.net

*Replace OFF by ON if you want to see more details in the Output..
SET MPRINT=OFF.

*////////////////////////////////////////// Beginning of Macro ////////////////////////////.

DEFINE !paired (sample	=!CHAREND('/')
		/control	=!CHAREND('/')
		/dv	=!CHAREND('/')
		/fname	=!DEFAULT("") !CMDEND)
GET FILE='C:\\Program Files\\SPSS\\test\\riskfact.sav'.

*Create the comparative criteria.
COMPUTE criter=0.
!DO !var !IN (!control)
+	COMPUTE criter=criter*10 + !var.
!DOEND.

EXECUTE.

SELECT IF(~MISSING(criter)).
COMPUTE draw=UNIFORM(1).
SORT CASES BY !sample criter.
RANK
  VARIABLES=draw  (A) BY !sample criter  /RANK /PRINT=YES
  /TIES=MEAN .

SORT CASES BY criter rdraw.

*compute the matching criteria.
COMPUTE criter2=criter*100000+rdraw.
SAVE OUTFILE='C:\\Program Files\\SPSS\\test\\all_data.sav'.
SELECT IF (!sample=1).
SAVE OUTFILE='C:\\Program Files\\SPSS\\test\\normal pop.sav'.

GET FILE='C:\\Program Files\\SPSS\\test\\all_data.sav'.
SELECT IF (!sample<>1).


!DO !var !IN (!control)
+	RENAME VARIABLES (!var=!CONCAT(!var,1)).
!DOEND.
!LET !dv1=!NULL.
!DO !var !IN (!dv)
+	RENAME VARIABLES (!var=!CONCAT(!var,1)).
+	!LET !dv1=!CONCAT(!dv1," ",!var,1)
!DOEND.

RENAME VARIABLES (lfnr=lfnr1) (rdraw=rdraw1).
SAVE OUTFILE='C:\\Program Files\\SPSS\\test\\pathological pop.sav'.

MATCH FILES /FILE=*
 /FILE='C:\\Program Files\\SPSS\\test\\normal pop.sav'
 /RENAME (!sample = d0)
 /BY criter2
 /DROP=d0.

* Next line is to keep only cases which have a match.
SELECT IF (~MISSING(lfnr1) AND ~MISSING(lfnr)).
EXECUTE.

!IF (!LENGTH(!fname) !NE 0) !THEN
SAVE OUTFILE=!QUOTE(!CONCAT('C:\\Program Files\\SPSS\\test\\',!fname,'.sav')).
!IFEND

T-TEST
  PAIRS= !dv  WITH !dv1 (PAIRED)
  /CRITERIA=CIN(.95)
  /MISSING=ANALYSIS.
!ENDDEFINE.
*////////////////////////////////////////// End of Macro /////////////////////////////////////.


**** Example no 1: call macro to study labfibg ****.

!paired 	sample	=labfibg 
	/control	=altgrp1 sex rr2grp diabet nikotin
	/dv	= Іbag Іher
	/fname	=labfibg controling for 5 var. 

*The macro variables are.
*sample		=the variable being studied.
*control		=the variables being controlled.
*dv 		=the dependent variables.
*fname		=the name of the file containing the matched data.
*		leave the name blank if this is not needed.

**** Example no 2: call macro to study l_cholgr ****.

!paired 	sample	= l_cholgr
	/control	=altgrp1 sex rr2grp nikotin gewicht 
	/dv	= Іbag Іher
	/fname	=. 

* Note that in example 2, fname is blank therefore the data file containing the matched data is not saved.