Search code examples
sassas-macro

Use do loop index in macro variable definition


I would like to define multiple macro variables in a data step. In this case I would like to create the variables &buffer1, &buffer2, &buffer3, &buffer4. The number of buffers is variable, so I am not able to hard code the creation of these variables.

Here is the data step and do loop I am currently using:

%let buffers = 4;
data buffer;
    do buffer_number = 1 to &buffers;
        buffer_queue = 0;
        buffer_index = 0;
        output;
    end;
run;

What I would like to do is add a line within the do loop like %let buffer_buffer_number = 0;. This obviously doesn't work, since it just creates the variable &buffer_buffer_number.Is there some way I can use the do loop index (buffer_number) to create the macro variables?


Solution

  • Try the call symput() subroutine.

    call symput(catt("buffer_",buffer_number),0);
    

    The first argument takes a string that contains the macro variable name. Here I use the CATT() function to concatenate the values into the string you want.

    The second argument is the value to put into the macro variable.