To address your problem and calculate the time difference correctly, you should convert the hours and minutes into the HH:MM 24-hour format, and then convert these time strings into actual SAS time values. Once you have the time values, you can easily calculate the difference between 'bedtime' and 'time to get up'. Here’s a step-by-step solution: Concatenate Hours and Minutes: You need to format your hours and minutes properly to ensure that they have two digits, particularly for values like "5" minutes, which should be "05". Convert the Time into SAS Time Format: Once you have the time in HH:MM format, you can use the INPUT function to convert it into a SAS time value. Calculate Time Difference: After converting the times, you can subtract them to find the difference, and format the result in hours and minutes. Here’s a code example that does this: /* Combine hours and minutes into HH:MM format for Bedtime */
BT = cats(put(C3_0, z2.), ":", put(C3_1, z2.));
/* Combine hours and minutes into HH:MM format for Time to get up */
TGU = cats(put(C3_2, z2.), ":", put(C3_3, z2.));
/* Convert the time strings into SAS time values */
BT_time = input(BT, time5.);
TGU_time = input(TGU, time5.);
/* Calculate the time difference, adjust for crossing midnight */
if TGU_time >= BT_time then Time_Diff = TGU_time - BT_time;
else Time_Diff = (TGU_time + '24:00't) - BT_time;
/* Format the result as hours and minutes */
format BT_time TGU_time Time_Diff time5.; Explanation: Concatenation: cats(put(C3_0, z2.), ":", put(C3_1, z2.)) ensures that hours and minutes are always two digits. SAS Time Value: input(BT, time5.) converts the concatenated string ( HH:MM ) into a SAS time value. Time Difference Calculation: If the time to get up is earlier than bedtime (crossing midnight), the code handles that by adding 24 hours ( '24:00't ). Formatting: The result is formatted in the HH:MM time format using the time5. format. This will give you the correct output with the time difference calculated in HH:MM .
... View more