BookmarkSubscribeRSS Feed
Max_Meier
Calcite | Level 5

Hello guys,

i try to merge a string and a macro variable into a new macro variable and hope you can help me.

The String is e.g. test.

The macro variable is a counter from 1 to 3:

%let counter = 1;

And my aim is to combine them into a new macro variable that is named: test1, test2, test3.

 

I tried it with "||", catx, but nothing worked :(.

 

I hope you understand what i want.

Thank you very much.

 

 

 

6 REPLIES 6
Max_Meier
Calcite | Level 5
%let counter1=1;
%let counter2=2;
%let counter3=3;

%let newmacro= test || &counter1;
%put newmacro;

*or;

%let newmacro1= catx("test", &counter1);
%put newmacro1;

 That's of course just the beginning, but even there the macro "newmacro" doesn't show "test1" what i would expect.

PaigeMiller
Diamond | Level 26

This line

 

%let newmacro1= catx("test", &counter1);

 

will not work because CATX is a data step function, and you are not working within a data step.

 

Macro variables will concatenate with other macro variables, or with plain text (such as your usage of the string 'test'), just by placing them next to one another.

 

%let newmacro1= test&counter1;

 

--
Paige Miller
Max_Meier
Calcite | Level 5

Ah thanks, that helped me a lot.

Now i get the correct name of the Macro variable. Is there any possibility to use this string as an own macro variable?

 

%let counter1=1;
%let test1=50000;

%let newmacro= test&counter1;

I mean i want to get the value 50000 from the newmacro variable?

xxformat_com
Barite | Level 11

Hi,

First note that you're working with "macro variables" and not "macros". That's two different things.

So I would use %let newmcrvar= rather than %let newmacro=.

 

Second, it is a good practice to add a dot at the end of a macro variable call. You will otherwise face some issues if you have dots in your string e.g. a dot in a filename (myfile.xlsx)

 

Third, when you use two ampersands one after the other, they are converted to a single ampersand.

So, in your case, you currently get "test1" as output but you actually want "&test1." which will be automatically converted to 5000 in the second internal loop.

I let you figure out the solution with this extra info.

Astounding
PROC Star

If test&counter1 resolves to test1, then &&test&counter1 would resolve to 5000.

 

But you already have a macro variable (test) that resolves to 5000.  Why do you need another one?

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 6 replies
  • 1661 views
  • 0 likes
  • 5 in conversation