How can I concatenate two variables in a Macro with an underscore between them

Accepted Solution Solved
Reply
Contributor
Posts: 41
Accepted Solution

How can I concatenate two variables in a Macro with an underscore between them

Hi there,

Suppose I have the following macro

%MACRO MacroA(varA, varB);

I would like to create a new dataset that should be named varA_varB. For example, if varA is hello and varB is world, the datasource would be hello_world.

I tried &varA + "_" &varB in my macro code, but SAS doesn't like that. How can I achieve this?

Regards,

P.


Accepted Solutions
Solution
‎01-26-2013 02:21 PM
Super User
Super User
Posts: 7,071

Re: How can I concatenate two variables in a Macro with an underscore between them

&vara._&varb

You do not need an operator to combine strings generated by macro code.  But you do need something to help the parser distinguish between whether you want the value of macro variable VARA or one named VARA_ .  So you need to add a period before the _.   Now if you really do need to have a period immediately after an expanded macro variable then you will have to type two as the first will be interpreted as ending the macro variable reference.

View solution in original post


All Replies
Super Contributor
Posts: 1,636

Re: How can I concatenate two variables in a Macro with an underscore between them

data class1 class2;

  set sashelp.class;

run;

%macro macroa(vara,varb);

data &vara._&varb;

  set &vara &varb;

  run;

%mend;

%macroa(class1,class2)

proc print data=&syslast;run;

Solution
‎01-26-2013 02:21 PM
Super User
Super User
Posts: 7,071

Re: How can I concatenate two variables in a Macro with an underscore between them

&vara._&varb

You do not need an operator to combine strings generated by macro code.  But you do need something to help the parser distinguish between whether you want the value of macro variable VARA or one named VARA_ .  So you need to add a period before the _.   Now if you really do need to have a period immediately after an expanded macro variable then you will have to type two as the first will be interpreted as ending the macro variable reference.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 3523 views
  • 5 likes
  • 3 in conversation