BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
pmdci
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

&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

2 REPLIES 2
Linlin
Lapis Lazuli | Level 10

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;

Tom
Super User Tom
Super User

&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.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 13135 views
  • 6 likes
  • 3 in conversation