DATA Step, Macro, Functions and more

SAS Error - %EVAL function or %IF

Reply
New Contributor
Posts: 2

SAS Error - %EVAL function or %IF

[ Edited ]

i keep getting the following error:-

 

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric
operand is required. The condition was: E_START_DATE&i not in ('') and &CYm1=0
ERROR: The macro MONTHELIG will stop executing.

 

Absolutely not able to debug this -

%macro monthelig;
	%let i=1;

	%do %while (&i <7);
		%let CYm1=0;

		data test;
			set test_0;

			%if E_START_DATE&i not in ('') and &CYm1=0 %then
				%do;

					if E_START_DATE&i<='01APR2018'd<=E_END_DATE&i then
						CYm1=1;
					else
						CYm1=0;
				%end;
			%else %if E_START_DATE&i not in ('') and &CYm1=1 %then
				%do;
					CYm1=1;
				%END;
			%else %if E_START_DATE&i in ('') and &CYm1=1 %then
				%do;
					CYm1=1;
				%END;
			%else %if E_START_DATE&i in ('') and &CYm1=0 %then
				%do;
					CYm1=0;
				%end;
		run;

		%let i=%sysevalf(&i+1);
		%put i=&i;
	%end;
%mend monthelig;

%monthelig;

 

Any help or suggestion, I would appreciate it very much. Thank you.

Super User
Posts: 22,874

Re: SAS Error - %EVAL function or %IF

Please reformat your code so it's correct, you seem to have text in the middle.

 

Turn on the debugging options from your program, run it and then post the full log with the errors so we can better help you debug your code. 

 

options mprint symbolgen;

Without your data we can't run the code.

 


@sghatak wrote:

i keep getting the following error:-

 

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric
operand is required. The condition was: E_START_DATE&i not in ('') and &CYm1=0
ERROR: The macro MONTHELIG will stop executing.

 

Absolutely not able to debug this -

%macro monthelig;
%let i=1;
%do %while (&i <7);
%let CYm1=0;
data test;

 

Any help or suggestion, I would appreciate it very much. Thank you.
set test_0;
%if E_START_DATE&i not in ('') and &CYm1=0 %then %do;
if E_START_DATE&i<='01APR2018'd<=E_END_DATE&i then CYm1=1;
else CYm1=0;
%end;
%else %if E_START_DATE&i not in ('') and &CYm1=1 %then %do;
CYm1=1;
%END;
%else %if E_START_DATE&i in ('') and &CYm1=1 %then %do;
CYm1=1;
%END;
%else %if E_START_DATE&i in ('') and &CYm1=0 %then %do;
CYm1=0;
%end;
run;
%let i=%sysevalf(&i+1);
%put i=&i;
%end;
%mend monthelig;
%monthelig;


 

Super User
Posts: 22,874

Re: SAS Error - %EVAL function or %IF

You also seem to have both a variable and macro variable that named CYM1?
New Contributor
Posts: 2

Re: SAS Error - %EVAL function or %IF

Thank you , i could figure out the issues. Corrected code:-

options mprint symbolgen;
%macro monthelig;
%let CYm1=0;
%let i=1;
%do %while (&i <7);
data test;
set test_0;
%if E_START_DATE&i<='01APR2018'd<=E_END_DATE&i and &CYm1=0 %then %do;
CYm1=1;
%end;
%else %if E_START_DATE&i not in ('') and &CYm1=1 %then %do;
CYm1=1;
%END;
%else %if E_START_DATE&i in ('') and &CYm1=1 %then %do;
CYm1=1;
%END;
%else %if E_START_DATE&i in ('') and &CYm1=0 %then %do;
CYm1=0;
%end;
run;
%let i=%sysevalf(&i+1);
%put i=&i;
%end;
%mend monthelig;
%monthelig;

Super User
Posts: 9,611

Re: SAS Error - %EVAL function or %IF

 


@sghatak wrote:

Thank you , i could figure out the issues. Corrected code:-

options mprint symbolgen;
%macro monthelig;
%let CYm1=0;
%let i=1;
%do %while (&i <7);
data test;
set test_0;
%if E_START_DATE&i<='01APR2018'd<=E_END_DATE&i and &CYm1=0 %then %do;
CYm1=1;
%end;
%else %if E_START_DATE&i not in ('') and &CYm1=1 %then %do;
CYm1=1;
%END;
%else %if E_START_DATE&i in ('') and &CYm1=1 %then %do;
CYm1=1;
%END;
%else %if E_START_DATE&i in ('') and &CYm1=0 %then %do;
CYm1=0;
%end;
run;
%let i=%sysevalf(&i+1);
%put i=&i;
%end;
%mend monthelig;
%monthelig;


This won't work either, for the reasons @Astounding already gave. You need to understand what the macro PREprocessor does before you start using it.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 6,543

Re: SAS Error - %EVAL function or %IF

First,  you need to understand that macro language has zero ability to inspect the value of a DATA step variable.  Your code expects that it can, but it just plain can't.  Never. 

 

Recommendation:  you would be best served by constructing a program with no macro language in it.  Use SAS language only, and get a version of your program that works the way you would want it to when &i is 1.  Once you have a working DATA step, we can talk about how to write a macro language loop.  But not before.

Ask a Question
Discussion stats
  • 5 replies
  • 99 views
  • 0 likes
  • 4 in conversation