SPSS Statistics

SPSS Statistics

Your hub for statistical analysis, data management, and data documentation. Connect, learn, and share with your peers! 

 View Only
Expand all | Collapse all

Identifying closest date to another date variable

  • 1.  Identifying closest date to another date variable

    Posted Tue October 18, 2022 02:01 PM

    I have a dataset formatted as follows, this is only an example: 

    ID

    PrescriptionFillDate

    BMIdate

    BMIvalue

    1

    8/1/2015

    4/2/2013

    29.9

    1

    9/4/2015

    6/3/2016

    22

    1

    10/7/2015

    1/4/2016

    25

    2

    2/8/2014

    4/4/2015

    20

    2

    6/9/2015

    8/9/2016

    33

    2

    10/23/16

    .

     

    2

    12/5/17

    .

     

    3

    1/3/2014

    9/3/2015

    27

     

    The prescription fill date and BMI date with respective BMI values are in long storage type. The number of prescription and BMI dates vary by study ID.

     I want to match the closest BMI date to the prescription fill date, with the respective BMI value, plus or minus one year.

     I have over 1 million observations and I need help in figuring out how to do this.

     Thank you,

     Alexandra



    ------------------------------
    Alexandra Rivera
    ------------------------------

    #SPSSStatistics


  • 2.  RE: Identifying closest date to another date variable

    Posted Tue October 18, 2022 02:31 PM
    Hi. Maybe something like this:

    data list list /ID (F4) PrescriptionFillDate BMIdate (2ADATE10) BMIvalue (F8.2).
    begin data.
    1 8/1/2015 4/2/2013 29.9
    1 9/4/2015 6/3/2016 22
    1 10/7/2015 1/4/2016 25
    2 2/8/2014 4/4/2015 20
    2 6/9/2015 8/9/2016 33
    2 10/23/16 .
    2 12/5/17 .
    3 1/3/2014 9/3/2015 27
    end data.
    
    compute diff=DATEDIFF(PrescriptionFillDate, BMIdate, "year").
    sort cases by id (A) diff (A).
    list id PrescriptionFillDate, BMIdate, diff.
    ​
    Obviously, I don't have the exact formats of your data so I just made that part up. But DATEDIFF does what I think you want.

    ------------------------------
    Rick Marcantonio
    Quality Assurance
    IBM
    ------------------------------