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
* Want to select only patients for whom drug1 was given before drug2.
* Raynald Levesque rlevesque@videotron.ca 2001/05/09.


*I assume your data file has one drug per case.

DATA LIST LIST /id(F) drug(A8) dtgiven(ADATE11).
BEGIN DATA
1 'drugA'  '01/01/2000'
1 'drugB'  '01/02/2000'
1 'drugC' '01/03/2000'
2 'drugB' '01/07/2000'
2 'drugA' '01/09/2000'
2 'drugC' '01/15/2000'
2 'drugB' '01/20/2000'
END DATA.
LIST.

* Save the original data file.
SORT CASES BY id.
SAVE OUTFILE='c:\\temp\\mydata.sav'.

* Select patients where drug1 was first given before drug2.
* I assume drug1 is never given the same day as drug2.

* Define a macro to do the job.

*/////////////////////////////////.
DEFINE !drug (drug1=!TOKENS(1) /drug2=!TOKENS(1))
GET FILE='c:\\temp\\mydata.sav'.
* Keep only the 2 relevant drugs.
SELECT IF ANY(drug,!drug1, !drug2).

SORT CASES BY id dtgiven.
MATCH FILES FILE=* /BY=id /first=first.
* First=1 means this is the first drug administered.
* next line keeps only id of patients for whom first drug was drug1.
SELECT IF (first=1) and (drug=!drug1).

MATCH FILES /FILE='c:\\temp\\mydata.sav'
 /TABLE=*
 /RENAME (drug dtgiven = d0 d1)
 /BY id
 /DROP= d0 d1.
* Keep only patients where drug1 was given before drug2.
SELECT IF first=1.
EXECUTE.
!ENDDEFINE.
*/////////////////////////////////.


**************************************.
* Test the macro with the data given above.
**************************************.

* This selects only patient with id=1.
!drug drug1='drugA' drug2='drugB'.
LIST.

* This selects both patients.
!drug drug1='drugB' drug2='drugC'.
LIST.

* This selects only patient with id=2..
!drug drug1='drugB' drug2='drugA'.
LIST.
EXECUTE.