Yes this is possible and there is, of course, more than one way to do this.
From your question it is unclear to me what exactly you want to have happen when the condition is met. Stop execution for this particular combination of macro variable values, stop the loop, stop the macro, stop the program. Lots of choices.
Assuming that you are trying to control for this particular combination, perhaps the simplest is to use the %IF-%THEN/%ELSE. Also in your %IF you have left off the & for the macro variable &YEAR.
> /*Stack up one after the other*/
> %macro stack_up;
> %local months month year;
> %let months=mar jun sep dec;
> %do year=1997 %to 2009 %by 1;
> %do month=1 %to 4 %by 1;
> %if year=1997 and %scan(&months,&month)=mar %then %do;
> /*Something like BREAK here to quit this iteration, but don't know which*/
> %end;
> %end;
> %mend stack_up;
Your %IF will be something like the following (depending on what you want to do - or not do for the stated condition.
[pre]
%if &year=1997 and %scan(&months,&month)=mar %then %do;
/* Assumes that you want to do something here and not stop the loop*/
%end;
%else %do;
/* what you want to do for other conditions goes here */
%end;
[/pre]
For other what-if scenarios consider the %ABORT and %RETURN statements. Or other %IF-%THEN logic.
... View more