Search code examples
randomtimesasgeneratordifference

How to generate random time in SAS and get time difference?


I am very new to SAS Programming. I have to create two variables for working hour calculation. I also have to use random time for this task. Here is what I have tried...

DATA wh;
in_1 = 28800;
in_2 = 36000;
out_1 = 18000;
out_2 = 25200;

DO i=1 TO 5;
  time_in = RAND("UNIFORM", in_1, in_2);
  time_out = RAND("UNIFORM", out_1, out_2);

  working_hour = INTCK('HOUR', time_out, time_in);
OUTPUT;
END;
RUN;

The random time generator works fine, but the INTCK function does not return expected values. I know it may be very silly. But I am stuck :(


Solution

  • Not sure what you are expecting. The time in is random between 8:00 and 10:00, yet time out is random between 5:00 and 7:00. Normal time frame computation of in to out would be a negative result! However, your use of arguments in INTCK are reversed, so there is that.

    Consider this sample code and output.

    What changes to output would you expect ?
    Specifically, would you want 1 for row 4 ? (6:43 to 8:39 is not two complete hours, yet INTCK with default arguments calculates two hours as from 6:00 to 8:00)

    The code uses time literals so the random time range edge points are more friendly to human readers.

    data have;
      call streaminit (2021);
    
      do time_clock_entry_id = 1 to 5;
        time_in  = rand("uniform", '05:00:00't, '07:00:00't);
        time_out = rand("uniform", '08:00:00't, '10:00:00't);
    
        working_hours = intck('hour', time_in, time_out);
        output;
      end;
    
      format time_in time_out time8.;
    run;
    

    enter image description here