www.sas.com > SAS UK > In the Know Homepage Search | Contact Us    
SAS UK Newsletter Banner SAS - The power to know(tm)  

How can I increment a macro loop by non-continuous values?

The following macro can be used to increment a loop by non-continuous numeric values, character values, or both; supplied as a parameters to the macro.

The macro works by using a do while loop to scan the supplied list of values for the nth “word” (or number) that it finds and assigns this “word” to a macro variable. The macro variable “i” will be available to the code that we need to execute. At the bottom of the loop we increment “j” by one so that we can scan for the next “word” in the string and so on. The loop will stop executing as soon as the scan returns a blank value.

In this simple example we are using the technique in order to print selected members of a library.

%macro test(lib,values);
    %local i j ;
    %let j = 1 ;
    %do %while(%scan(&values,&j) ne ) ;
        %let i = %scan(&values,&j) ; %* <== Code that uses &i goes here ;
          Title "Listing of member &lib..&i ";
          proc print data=&lib..&i;
          run;
         %let j = %eval(&j+1) ;
    %end ;
%mend ;

%test(sashelp,class air)

Note: The above code has been tested using SAS 8.2 on the Windows operating system.