Looping through tokens in a macro variable with Scan

From sasCommunity
Jump to: navigation, search
%macro scanloop(elements);
  %let j=1;
  %let key=%scan(&elements.,&j.);
  %do %until(&key eq %nrstr( ));
    
    ... do something with &key ...

    %let j=%eval(&j+1);
    %let key=%scan(&elements.,&j.);
  %end;
%mend;

Example usage, where the "do something" part just puts the key to the log:

%scanloop(AAAA BBBB CCCC);
AAAA
BBBB
CCCC