## calculate a time interval using macro variables

Solved
Occasional Contributor
Posts: 10

# calculate a time interval using macro variables

I'm stuck trying to calculate the time interval between &systime and the current time, in minutes.  I've looked for examples in books and online, but they all seem to focus on using &sysdate or &systime in a title or footnote.  I'm new to macros, and I'm running version 6.1 of SAS on Demand for Professionals on a Lenovo PC with Windows 8.1.

Accepted Solutions
Solution
‎05-01-2014 07:41 PM
Super User
Posts: 8,120

## Re: calculate a time interval using macro variables

&SYSTIME is time part of when the SAS session started.  &SYSDATE (or &SYSDATE9) is the datepart.

DATETIME() function can be used to find the current time.

So to find the number of seconds just take the difference of the two.

%let seconds = %sysevalf(%sysfunc(datetime())-"&sysdate9:&systime"dt) ;

Just divide by 60 to get the number of minutes.

All Replies
Solution
‎05-01-2014 07:41 PM
Super User
Posts: 8,120

## Re: calculate a time interval using macro variables

&SYSTIME is time part of when the SAS session started.  &SYSDATE (or &SYSDATE9) is the datepart.

DATETIME() function can be used to find the current time.

So to find the number of seconds just take the difference of the two.

%let seconds = %sysevalf(%sysfunc(datetime())-"&sysdate9:&systime"dt) ;

Just divide by 60 to get the number of minutes.

Occasional Contributor
Posts: 10

## Re: calculate a time interval using macro variables

I tried it and it worked.  Thanks!

Super User
Posts: 13,583

## Re: calculate a time interval using macro variables

Is there any chance that this program will ever be run with a session that starts before midnight and runs after midnight? You'll need to incorporate the date parts to get proper results.

Super User
Posts: 23,771

## Re: calculate a time interval using macro variables

Time in sas is in seconds, so 60 is 60 seconds and 180 is 3 minutes.

You can do straight additions/subtraction with the time variables and then divide by 60 to get the number of minutes.

For evaluating functions in macro language you'll need to use %eval to resolve the subtraction.

SAS(R) 9.2 Macro Language: Reference

🔒 This topic is solved and locked.