Search code examples
sassas-macrosas-dis

Using SAS SET statement with numbered macro variables


I'm trying to create a custom transformation within SAS DI Studio to do some complicated processing which I will want to reuse often. In order to achieve this, as a first step, I am trying to replicate the functionality of a simple APPEND transformation.

To this end, I've enabled multiple inputs (max of 10) and am trying to leverage the &_INPUTn and &_INPUT_count macro variables referenced here. I would like to simply use the code

data work.APPEND_DATA / view=work.APPEND_DATA;
   %let max_input_index = %sysevalf(&_INPUT_count - 1,int);
   set &_INPUT0 - &&_INPUT&max_input_index;
   keep col1 col2 col3; 
run;

However, I receive the following error:

ERROR: Missing numeric suffix on a numbered data set list (WORK.SOME_INPUT_TABLE-WORK.ANOTHER_INPUT_TABLE)

because the macro variables are resolved to the names of the datasets they refer to, whose names do not conform to the format required for the

SET dataset1 - dataset9;

statement. How can I get around this?

Much gratitude.


Solution

  • You need to create a macro that loops through your list and resolves the variables. Something like

    %macro list_tables(n);
       %do i=1 %to &n;
          &&_INPUT&i
       %end;
    %mend;
    
    data work.APPEND_DATA / view=work.APPEND_DATA;
       %let max_input_index = %sysevalf(&_INPUT_count - 1,int);
       set %list_tables(&max_input_index);
       keep col1 col2 col3; 
    run;