DATA Step, Macro, Functions and more

SAS Macro error with time function

Reply
Super Contributor
Posts: 275

SAS Macro error with time function

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.

Respected Advisor
Posts: 3,156

Re: SAS Macro error with time function

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

Trusted Advisor
Posts: 1,913

Re: SAS Macro error with time function


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

Trusted Advisor
Posts: 1,913

Re: SAS Macro error with time function

Posted in reply to PaigeMiller

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

Ask a Question
Discussion stats
  • 3 replies
  • 1071 views
  • 1 like
  • 3 in conversation