BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
djbateman
Lapis Lazuli | Level 10

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?

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

View solution in original post

6 REPLIES 6
PGStats
Opal | Level 21

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
mkeintz
PROC Star

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;

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Haikuo
Onyx | Level 15

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

Ksharp
Super User

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

data_null__
Jade | Level 19

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

djbateman
Lapis Lazuli | Level 10

My example was simplified just for illustration purposes.  I would not normally use a macro if my example was all I was doing.  I just needed some general help.  But thank you for taking a look at my question!

sas-innovate-2024.png

Today is the last day to save with the early bird rate! Register today for just $695 - $100 off the standard rate.

 

Plus, pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 12251 views
  • 4 likes
  • 6 in conversation