2
votes

So I want to run nested loop for my macro function. Here's my code, it seem like SAS doesn't like by -1. Is there anyway I code this to let the second loop decrease step by -1? In this case, my yearMix = 1982 and yearMax = 1994.

%Macro theLoop;
    %Do I = &yearMin+1 %to &YearMax-1;
        %Do J = &YearMax-1 %to &I by -1;
            %Meaw;
        %END;
    %END;
%MEND theLoop;
%theLoop;

I got this error:

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: &I by -1
ERROR: The %TO value of the %DO J loop is invalid.
ERROR: The macro THELOOP will stop executing.

2

2 Answers

5
votes

You specify your increment in a macro %do loop using %by rather than by. Further details can be found in the user guide here.

In your code SAS is trying to evaluate &I by -1 as a numerical value.

0
votes
%let yearMin = 1982;
%let yearMax = 1994;

%Macro theLoop;
    %Do I = %eval(&yearMin+1) %to %eval(&YearMax-1);
        %Do J = %eval(&YearMax-1) %to &I %by -1;
            %put &i =  &j = ;
        %END;
    %END;
%MEND theLoop;
%theLoop;