Search code examples
sasconcatenationrenamesas-macroproc-sql

Concatenate macro variables in SAS to rename a column


I have some macro variables that I want to concatenate together to rename a column in a table.

%let input_group = state;
%let input_id = zip;

data output;
set output;
rename var1= catx(_, &input_id, &input_group, '1');
run;

In my output table I want to rename the var1 column as zip_state_1. Is it possible to do this in a SAS datastep? It would be even better if I could do this in a proc sql step like below.

%let input_group = state;
%let input_id = zip;

proc sql;
create table output_2 as
select var1 as catx(_, &input_id, &input_group, '1')
from output_1;

Thanks!


Solution

  • There is no need to use functions to concatenate values of macro variables. Just expand the macro variable where you want to use the value in the code you are generating.

    So if your macro variable values look like:

    %let input_id = zip;
    %let input_group = state;
    

    And you write this code:

    &input_id._&input_group._1
    

    The result will be

    zip_state_1
    

    Notice the use of . to let the macro processor know that the _ is not part of the name of the macro variable being expanded by the &.