Search code examples
sassas-macro

Macro execution for only odd values


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?


Solution

  • 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