Hi SAS community,
I have a question that needs your wisdom.
I want to execute a macro and feed the macro with the following three variables: start, end, and incre.
All these three variables take two decimal points.
As shown below
%macro loop(start=,end=,incre=);
%local weight;
%do weight = &start %to &end %by &incre;
XXXXXXXXXXXXXX;
XXXXXXXXXXXXXX (macro code);
XXXXXXXXXXXXXX;
%end;
%mend;
%macro(start= 0.5 ,end=0.99 ,incre=0.1 );
But, there is an error msg when I execute the code above, the error msg says the following:
A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:
.5
I think it's because the values I feed into start, end, and incre variables are taken as character, rather than numeric
What are the solutions to this problem?
Thank you all
I think it's because the values I feed into start, end, and incre variables are taken as character, rather than numeric
No. Macro variables are ALWAYS character.
The reason it doesn't work is because the iterative %DO loop only works for INTEGERS. The implicit %EVAL() function call used in %DO var= ... (and in %IF and %UNTIL and %WHILE) can only do arithmetic on strings that look like integers.
If you want to make your own looping construct for floating point values then build it using %SYSEVALF().
%let weight=&start;
%do %while (%sysevalf(&weight <= &end));
....
%let weight=%sysevalf(&weight + &incre);
%end;
Macro %DO loops do not permit any characters, even decimal points. You would have to switch to:
%loop (start=50, end=99, incr=10)
Then within the logic of the macro, divide these values by 100 as needed, to restore the proper logic.
You might more completely describe what this code is supposed to do. Most of the time I when I see multiple variables with decimal values what is being done more appropriately belongs in a data step.
Create your macro for a single instance, and use a data step loop with call execute().
I think it's because the values I feed into start, end, and incre variables are taken as character, rather than numeric
No. Macro variables are ALWAYS character.
The reason it doesn't work is because the iterative %DO loop only works for INTEGERS. The implicit %EVAL() function call used in %DO var= ... (and in %IF and %UNTIL and %WHILE) can only do arithmetic on strings that look like integers.
If you want to make your own looping construct for floating point values then build it using %SYSEVALF().
%let weight=&start;
%do %while (%sysevalf(&weight <= &end));
....
%let weight=%sysevalf(&weight + &incre);
%end;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.