BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
changxuosu
Quartz | Level 8

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

 

View solution in original post

6 REPLIES 6
Astounding
PROC Star

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.

ballardw
Super User

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.

changxuosu
Quartz | Level 8
thanks a lot! I studied execute() it's a very useful tool. First time to use it. Thanks for introducing me to this useful tool!
Tom
Super User Tom
Super User

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;

 

changxuosu
Quartz | Level 8
thanks a lot Tom. It works perfect!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 6 replies
  • 2494 views
  • 4 likes
  • 5 in conversation