datedifferencenegative-numberspss-modeler

spss-modeler date difference getting negative results


First of all, I'm new to SPSS modeler, sorry if my question sound too obvious for experts, but what I'm trying is to calculate the date difference between two date values ( arrive_Date , Depart_Date ) in (Hours), I used this function time_hours_difference , this function works okay if the Arrive_Date and depart_date are the same day, but if the days are different, the date difference is in negative value, I searched and enabled this option: Rollover days/mins in stream properties, but the problem remains the same. I hope you can help me with this beginner question.

Thanks


Solution

  • Hope its not too late. You are getting negative values because time_hours_difference extract only the time from the two specified timestamps and computes the difference out of it. It doesn't take the date values into account. For example:

    time_hours_difference('2012-08-02 14:10:50,2012-08-05 13:36:26) 
    

    Output:

    -0.573
    

    Remember, here time_in_hours('0130') under time format 'HHMM' evaluates to 1.5.

    Here is my solution: in order to take days into account, first extract date_days_difference and multiply it with 24 to bring it in Hours and then sum it up with the time difference using time_hours_difference.

    date_days_difference(arrive_Date,Depart_Date)*24 + 
    time_hours_difference(arrive_Date,(to_string(datetime_date(arrive_Date))  >< " 23:59:00"))   +   
    time_hours_difference((to_string(datetime_date(Depart_Date))  ><  " 00:00:00"),Depart_Date)