Statistical programming, matrix languages, and more

Problem with time() function

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

Problem with time() function

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



Accepted Solutions
Solution
‎10-03-2011 08:17 AM
SAS Super FREQ
Posts: 3,234

Re: Problem with time() function

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


All Replies
Solution
‎10-03-2011 08:17 AM
SAS Super FREQ
Posts: 3,234

Re: Problem with time() function

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?"

Occasional Contributor
Posts: 17

Problem with time() function

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.

Frequent Contributor
Posts: 104

Re: Problem with time() function

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.

Occasional Contributor
Posts: 17

Re: Problem with time() function

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.

Post a Question
Discussion Stats
  • 4 replies
  • 267 views
  • 3 likes
  • 3 in conversation