BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
TomTom
Calcite | Level 5

Hello,

I have a problem with the time() function. My code looks like this:

PROC IML;

    t0 = time();

    /* Step 1 computation */

    t1 = time() - t0;

    /* Step 2 computation */

    t2 = (time() - t0)-t1;

    msg = "Step 1: _x seconds   Step 2: _y seconds";

    sX = putn(t1, "BEST5.");

    sY = putn(t2, "BESTD9.");

    msg = tranwrd(msg, "_x", sX);

    msg = tranwrd(msg, "_y", sY);

    PRINT msg[label="Time evaluation"];

QUIT;

The time for step 1 is shown correctly but step 2 usually is not. I get all sorts of numbers for t2 including negative ones.  I suppose the problem is that step 2 takes considerably longer than step 1 (20 to 30 hours for step 2, <1 hour for step 1). Is the time function unable to handle such long timeframes or could it be that I just don’t get the format in the putn function right? I tried several BESTxx.x values but none seemed to work. The code works fine for 2 quick computations.

Many thanks


1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

The simpler way is to use

t0 = time(); /* overwrite t0; we don't need the original value */

/* Step 2 computation */

t2 = time()-t0;

I think your computation is equivalent, so it must be that your jobs are crossing midnight.

If so, use DATETIME instead of TIME:

d0=datetime();

/* long computation */

d1=datetime()-d0;

Perhaps the more relevant question is, "Why is your computation taking so long?"

View solution in original post

4 REPLIES 4
Rick_SAS
SAS Super FREQ

The simpler way is to use

t0 = time(); /* overwrite t0; we don't need the original value */

/* Step 2 computation */

t2 = time()-t0;

I think your computation is equivalent, so it must be that your jobs are crossing midnight.

If so, use DATETIME instead of TIME:

d0=datetime();

/* long computation */

d1=datetime()-d0;

Perhaps the more relevant question is, "Why is your computation taking so long?"

TomTom
Calcite | Level 5

Hi Rick, thanks for your swift reply.

Are you saying that i should put

     t0 = time(); /* overwrite t0; we don't need the original value */

immediately before the step 2 computation?

To be honest, I fail to see the difference between your t2 and mine.

In my code t2 = (time() - t0) is an elapsed time, like in yours, but it is the time from start to finish of the whole programme. From this I am subtracting the elapsed time for the first step. Can I not use elapsed times in calculations? I'd greatly appreciate it if you could elaborate.

DLing
Obsidian | Level 7

I think what Rick is saying is that time() is just a 24 hour clock value, and doesn't handle the clock advancing to the next day, hence your getting negative values.  The datetime() function does include date and time together so it should give you the corrrect result.

For example, if your job started at 8pm and finished next day at 7pm, you want 23 hours, but time of 8pm minus time of 7pm, to SAS, is -1 hours, i.e., 1 hour before.  TIme() has no notion of days.

TomTom
Calcite | Level 5

Hmm, I missed the second part of Rick's answer. Sorry about that. Actually, I was replying to the notification email I received for his post and the reply in there looks a bit different (i.e. doesn't contain the datetime bit). I'll try the datetime() function - thanks!

To answer the question why the computation takes so long: I am running what I would call a fairly complex Monte-Carlo simulation. By the way, thanks for implementing the submit statement allowing me to call procedures from within IML. I am using it for these simulations and it makes my life so much easier.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 4 replies
  • 1336 views
  • 3 likes
  • 3 in conversation