3

I am trying to implement a macro that takes only odd values into the algorithm. My strategy thus far is as follows:

%macro TEST;
    %do i=1 %TO 5;
        %IF %SYSFUNC(MOD(&i,2)=1) %THEN %DO;
        ALGORITHM 
    %END 
%END 

%MEND TEST;

%TEST

But I receive several errors stating that the 'Macro keyword do appears as text', among others. How might I solve this?

falling_up
  • 81
  • 9

4 Answers4

3

Since no-one else has suggested this, how about using %by? E.g.

%macro TEST;
    %do i=1 %TO 5 %by 2;
        %put i = &i;
    %END;
%MEND TEST;

%TEST
user667489
  • 9,501
  • 2
  • 24
  • 35
1

You need to do an evaluation on the logical value

%IF %SYSEVALF(%SYSFUNC(MOD(&i,2))=1,BOOLEAN) %THEN %DO;

You have a few syntax errors, here is a version that works:

%macro TEST();
    %do i=1 %TO 5;
        %IF %sysevalf(%SYSFUNC(MOD(&i,2))=1,boolean) %THEN %DO;
            %put &i - ALGORITHM ;
        %END ;
    %END ;

%MEND TEST;

%TEST();

Produces:

15014  %TEST();
1 - ALGORITHM
3 - ALGORITHM
5 - ALGORITHM
DomPazz
  • 12,415
  • 17
  • 23
0

Your code is missing a lot of semi-colons. You also have placed the =1 test into the middle of your %sysfunc() macro function call.

%macro TEST;
%do i=1 %TO 5;
  %IF %SYSFUNC(MOD(&i,2))=1 %THEN %DO;
    %put &i is odd ;
  %END ;
%END;

%MEND TEST;

%TEST;
Tom
  • 47,574
  • 2
  • 16
  • 29
0

Since 0 (and missing) evaluates to false, and anything else to true, you can simply remove the "=1".

%macro TEST;
    %do i=1 %to 5;
        %if %sysfunc(mod(&i,2)) %then %do;
            %put &i.;
        %end;
    %end;
%mend TEST;

%TEST;
ocstl
  • 386
  • 1
  • 3
  • 10