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.
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
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
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));
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.
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.