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.
Show us your code please.
%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.
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;
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?
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.
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?
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
Get started using SAS Studio to write, run and debug your SAS programs.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.