## %do loop with fraction increments

Solved
Regular Contributor
Posts: 247

# %do loop with fraction increments

I am trying to use a %do loop in a macro, but it does not appear that I can increment by fractions.  I am trying to increment by 0.25, but I get the following errors:

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition

was: 0.5

ERROR: The %BY value of the %DO K loop is invalid.

Here is an example of a macro that works:

%macro agree();

data test;

%do k=1 %to 10 %by 2;

result=&k.;

output;

%end;

run;

%mend agree;

%agree();

Here is an example of a macro that does not work when I change the %by value to a fraction:

%macro agree();

data test;

%do k=1 %to 10 %by 0.25;

result=&k.;

output;

%end;

run;

%mend agree;

%agree();

This is not an example of what I am doing.  I am just trying to illustrate that the loop doesn't increment on fraction.  Is it possible to use a %do loop while incrementing by a fraction?

Accepted Solutions
Solution
‎08-17-2012 03:05 PM
Posts: 5,526

## Re: %do loop with fraction increments

Macro arithmetic is integer only. You could use :

%macro agree();

data test;

%do k=100 %to 1000 %by 25;

result=&k./100;

output;

%end;

run;

%mend agree;

PG

PG

All Replies
Solution
‎08-17-2012 03:05 PM
Posts: 5,526

## Re: %do loop with fraction increments

Macro arithmetic is integer only. You could use :

%macro agree();

data test;

%do k=100 %to 1000 %by 25;

result=&k./100;

output;

%end;

run;

%mend agree;

PG

PG
Posts: 1,337

## Re: %do loop with fraction increments

Correct

But you can produce the needed K values like this:

%macro agree();

%let f=0.25;

%let inc = %sysfunc(ceil(1/&f));

%do Kinc= &inc to  10*&inc;

%let k=%sysevalf(&f*&kinc);

%put Kinc=&kinc  K=&k;

%end;

%mend;

Posts: 3,167

## Re: %do loop with fraction increments

wish there is an system option that one can switch the default %eval that micro processor uses to %sysevalf. Haikuo

Super User
Posts: 10,778

## Re: %do loop with fraction increments

PG is smart and sharp. Here is another way.

```%macro agree;
data test;
%let k=1;
%do %while(%sysevalf(&k le 10)) ;
result=&k. ;
output;
%let k=%sysevalf(&k+ 0.25);
%end;
run;
%mend agree;
options mprint;
%agree

```

Ksharp

Posts: 3,852

## Re: %do loop with fraction increments

There is nothing in your example to suggest you need macro language.

Regular Contributor
Posts: 247