1
votes

I have a dataset as such:

Case #|DateA |Drug.1|Drug.2|Drug.3|DateB.1 |DateB.2  |DateB.3 |IV.1|IV.2|IV.3
------|------|------|------|------|--------|---------|--------|----|----|----
1     |DateA1|    X |    Y |    X |DateB1.1|DateB1.2 |DateB1.3| 1  |  0 | 1
2     |DateA2|    X |    Y |    X |DateB2.1|DateB2.2 |DateB2.3| 1  |  0 | 1
3     |DateA3|    Y |    Z |    X |DateB3.1|DateB3.2 |DateB3.3| 0  |  0 | 1
4     |DateA4|    Z |    Z |    Z |DateB4.1|DateB4.2 |DateB4.3| 0  |  0 | 0

For each case, there are linked variables i.e. Drug.1 is linked with DateB.1 and IV.1 (Indicator Variable.1); Drug.2 is linked with DateB.2 and IV.2, etc.

The variable IV.1 only = 1 if Drug.1 is the case that I want to analyze (in this example, I want to analyze each receipt of Drug "X"), and so on for the other IV variables. Otherwise, IV = 0 if the drug for that scenario is not "X".

I want to calculate the difference between DateA and DateB for each instance where Drug "X" is received.

e.g. In the example above I want to calculate a new variable:

DateDiffA1_B1.1 = DateA1 - DateB1.1
DateDiffA1_B2.1 = DateA1 - DateB2.1
DateDiffA1_B1.3 = DateA1 - DateB1.3
DateDiffA1_B2.3 = DateA1 - DateB2.3
DateDiffA1_B3.3 = DateA1 - DateB3.3

I'm not sure if this new variable would need to be linked to each instance of Drug "X" as for the other variables, or if it could be a single variable that COUNTS all the instances for each case.

The end goal is to COUNT how many times each case had a date difference of <= 2 weeks when they received Drug "X". If they did not receive Drug "X", I do not want to COUNT the date difference.

I will eventually want to compare those who did receive Drug "X" with a date difference <= 2 weeks to those who did not, so having another indicator variable to help separate out these specific patients would be beneficial.

I am unsure about the best way to go about this; I suspect it will require a combination of IF and REPEAT functions using the IV variable, but I am relatively new with SPSS and syntax and am not sure how this should be coded to avoid errors.

Thanks for your help!

EDIT: It seems like I may need to use IV as a vector variable to loop through the linked variables in each case. I've tried the syntax below to no avail:

DATASET ACTIVATE DataSet1.
vector IV = IV.1 to IV.3.
    loop #i = .1 to .3.
do repeat DateB = DateB.1 to DateB.3 
                / DrugDateDiff = DateDiff.1 to DateDiff.3.
    if IV(#i) = 1
    / DrugDateDiff = datediff(DateA, DateB, "days").
end repeat.
end loop.
execute.
1

1 Answers

1
votes

Actually there is no need to add the vector and the loop, all you need can be done within one DO REPEAT:

compute N2W=0.
do repeat DateB = DateB.1 to DateB.3 /IV=IV.1 to IV.3 .
    if IV=1 and datediff(DateA, DateB, "days")<=14 N2W = N2W + 1.
end repeat.
execute.

This syntax will first put a zero in the count variable N2W. Then it will loop through all the dates, and only if the matching IV is 1, the syntax will compare them to dateA, and add 1 to the count if the difference is <=2 weeks.

if you prefer to keep the count variable as missing when none of the IV are 1, instead of compute N2W=0. start the syntax with:

If any(1, IV.1 to IV.3) N2W=0.