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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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