Question

I want to run a while/until loop in a macro function, and have its maximum number of iterations limited. I found how to do it in 'usual' sas :

 data dataset;
    do i=1 to 10 until(condition);      /*10 iterations max */
        /* stuff */
    end;
 run;

but if I try it in a macro function :

 %macro mf;
 data dataset;
 %do i=1 %to 10 %until(nrow(X)>10);      /*10 iterations max */
 /* stuff */
 %end;
 run;
 %mend;

%mf;

I get these errors :

ERROR: Improper use of macro reserved word until.
ERROR: A dummy macro will be compiled.
ERROR: Required operator not found in expression: 10 %until(nrow(X)>10)
ERROR: The %TO value of the %DO I loop is invalid.
ERROR: The macro MF will stop executing.

What is the proper way to limit loops iterations in macro-functions ?

Here is a data set in case you want to test ideas :

DATA dataset;
input X Y Z;
cards;
10 0 20
50 20 60
90 60 30
run;
Was it helpful?

Solution

The following is an example of what you can use:

%macro mf;
  %let i=0;
  %do %until(&onechar=e or &i=10);
    %let i=%eval(&i+1);
    %let onechar=%substr(abcdefghij,&i,1);
  %end;
  %put onechar=&onechar;
  %put i=&i;
%mend mf;
%mf;

The macro loop stops if it finds "e" or i=10.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top