SPSS Statistics

SPSS Statistics

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

 View Only
  • 1.  How can SPSS Split Time Range by the Hour for each Row?

    Posted Mon October 17, 2022 10:28 AM
    Hello everyone and thank you in advance for your time,

    I have a data set similar to the one below. I want to get the duration for each participant AND for each time slot.
    For example:
    • Participant 1
      • Slot 8
        • Duration 3min
      • Slot 19
        • Duration 5min (sum of duration from both rows: 3 and 4)
    The problem is for the rest of the rows.
    In Row 5:
    • Participant 2
      • Duration is actually 1m 28sec but the Slot should be split in slot 19 and slot 20
      • Slot 19
        • Duration 1m 6sec
      • Slot 20
        • Duration 22sec

    Is there anyway SPSS can help me duplicate such rows and change their startTime and endTime such as:

    Thank you!

    ------------------------------
    Bruna Kapaj
    ------------------------------

    #SPSSStatistics


  • 2.  RE: How can SPSS Split Time Range by the Hour for each Row?

    Posted Mon October 17, 2022 11:58 AM
    Hi.

    I first was thinking of suggesting the VARSTOCASES command for at least part of this task, but since your 'otherSlots' variable can take multiple arguments, that's not going to work.

    I think a Python solution is the only feasible one.

    It will take me some time to get to it. Jon Peck is such an expert that he may already have a solution half-way or completely written by now. In any event, if you know Python, I would suggest giving it a try in a BEGIN PROGRAM / END PROGRAM block.

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



  • 3.  RE: How can SPSS Split Time Range by the Hour for each Row?

    Posted Mon October 17, 2022 01:31 PM
    I'm happy to help, but I'm not sure about the logic.  I guess slot means hour, but can you elaborate?

    --





  • 4.  RE: How can SPSS Split Time Range by the Hour for each Row?

    Posted Mon October 17, 2022 02:24 PM
    Hello and thank you Rick and Jon!

    @Jon - By slot I mean the hour of the startTime

    If the startTime and endTime have the same slot = that is OK
    If the startTime and endTime have different slots (are in different hours) -> I want to duplicate the whole row but just change the startTime and endTime like in the 2nd picture -> Split Time Range by the Hour for each Row

    I hope this is more clear.
    Thank you so much!

    Kind regards,
    Bruna


    ------------------------------
    Bruna Kapaj
    ------------------------------



  • 5.  RE: How can SPSS Split Time Range by the Hour for each Row?

    Posted Mon October 17, 2022 01:46 PM
    Oh, I see.  Let me think about it.

    On Mon, Oct 17, 2022 at 11:30 AM Jon Peck <jkpeck@gmail.com> wrote:
    I'm happy to help, but I'm not sure about the logic.  I guess slot means hour, but can you elaborate?

    --


    --





  • 6.  RE: How can SPSS Split Time Range by the Hour for each Row?

    Posted Tue October 18, 2022 01:23 PM
    Following is code that does most of the work.  First it creates new variables that contain a slot number (hour) and the number of seconds in that slot.  Then it uses varstocases to convert those into a case for each id and slot along with whatever other variables are in the file.  No Python code needed as the time functions in Statistics can handle all this.

    From here, you can adjust the start and end times however you like using that information.  Note that when the times wrap, the end time will exceed 24.

    This code allows for 24 slots.  You can use a SELECT IF command to get rid of the empty records, i.e., those where the slot gets no time, if you want.

    * Encoding: UTF-8.
    compute starthour = trunc(ctime.hours(startTime)).
    compute endhour = trunc(ctime.hours(endTime)).

    do if endhour < starthour.
        compute endhour = endhour + 24.
        compute endTime = endTime + 24 * 60 * 60.
    end if.
    COMPUTE #diffinsecs=datediff(endTime, startTime, "seconds").

    vector v(24).
    vector slot(24).
    loop #i = 1 to 24.
    do if #i eq 1.
    * up to next hour.
    compute v(1) = min((starthour+1)*60*60 - ctime.seconds(startTime), #diffinsecs).
    else.
    compute v(#i) = min(#diffinsecs, 3600).
    end if.
    compute #diffinsecs = #diffinsecs - v(#i).
    compute slot(#i) = mod(starthour + #i - 1, 24).
    end loop.
    exec.

    varstocases /make slots from slot1 to slot24
        /make seconds from v1 to v24.
    --





  • 7.  RE: How can SPSS Split Time Range by the Hour for each Row?

    Posted Sat October 22, 2022 04:18 AM
    Hello Jon,

    It worked like a charm.
    Thank you so much for your support. Very much appreciated!

    All the best to you!



    ------------------------------
    Bruna Kapaj
    ------------------------------