BookmarkSubscribeRSS Feed
saspert
Pyrite | Level 9

Hi,

I am trying to calculate the hourly time intervals for the past 24 hours using a loop. Can anyone help me with resolving the error?

Should I rather use a datastep to acheive this?

%macro _createtimeintervals;
%do i = 1 %to 24;
%let j = %eval( -&i.);
%put "i = &i. and j=&j.";
%let nexthr =%sysfunc(intnx('hour',time(),%eval(&i.)));
%put "next hour = &nexthr.";
%end;
%mend _createtimeintervals;
%_createtimeintervals


ERROR: Required operator not found in expression: time()
ERROR: Argument 2 to function INTNX referenced by the %SYSFUNC or %QSYSFUNC macro function is not a number.
ERROR: Invalid arguments detected in %SYSCALL, %SYSFUNC, or %QSYSFUNC argument list.  Execution of %SYSCALL statement or %SYSFUNC
       or %QSYSFUNC function reference is terminated.

Thanks,

saspert.

3 REPLIES 3
Haikuo
Onyx | Level 15

Well, there are several issues in your code

1. Replace 'hour' with hour. In macro, you don't need to quote it.

2. Time() needed to be wrapped with %sysfunc()

3. This is not a problem, since you are NOT doing any arithmetic operations,  you don't need %eval to wrap up &i.

4. This is also not necessarily a problem, but you may want to add a format for your time.

%macro _createtimeintervals;

%do i = 1 %to 24;

%let j = %eval( -&i.);

%put "i = &i. and j=&j.";

%let nexthr =%sysfunc(intnx(hour,%sysfunc(time()),&i.), time.);

%put "next hour = &nexthr.";

%end;

%mend _createtimeintervals;

%_createtimeintervals

Haikuo

PaigeMiller
Diamond | Level 26


As far as I have been using %sysfunc with INTNX (and maybe there are better ways)

You need a separate line in the code where time() is assigned to a macro variable, and in macro code, you don't put 'hour' in quotes

Also, you don't need quotes in your %put statement and &j doesn't seem to play any useful role here, so the simplified and working macro should be

%macro _createtimeintervals;

%do i = 1 %to 24;

%let abc=%sysfunc(time());

%let nexthr=%sysfunc(intnx(hour,&abc,&i));

%put nexthr &nexthr;

%end;

%mend _createtimeintervals;

%_createtimeintervals

--
Paige Miller
PaigeMiller
Diamond | Level 26

If you want a single line of code, rather then two lines of code mentioned above, you could use:

%let nexthr=%sysfunc(intnx(hour,%sysfunc(time()),&i));

--
Paige Miller

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 2588 views
  • 3 likes
  • 3 in conversation