Hello,
How do I assign a macro variable a value of a variable?
I want to make 'macro_variable' contain the value of a non macro variable.
In other words, if a variable value is 'ABC123' then I want the macro_variable to contain 'ABC123'.
So like:
%let macro_variable = the value stored within (variable);
Because I then want to be able to create new variables called:
case_¯o_variable
which would resolve to a variable called case_ABC123 which I can then in turn assign values to.
Regards,
Mark
Use function symput to assign a variable data into a macro variable:
call symput('<macro_var_name' , <variable_name>);
there are also functions symputx and symputn wit similar sytax, to assign CHAR or NUM type variable.
check documentation.
Can you post example data of what you have and what you want as result?
In a data-step you can use call symputx('macro_variable', value), but be aware that you will only get value of the last observation. And, if you want to use a value as variable name, you have to check whether that value (including the prefix "case_") is still a valid variable name.
Use function symput to assign a variable data into a macro variable:
call symput('<macro_var_name' , <variable_name>);
there are also functions symputx and symputn wit similar sytax, to assign CHAR or NUM type variable.
check documentation.
I read @Shmuel's answer and thought "symputn, what the heck is that?" After 5 minutes of googling, turns out it's only valid for SCL programs, not DATA step programs. But it's a good reminder to me to keep rereading the documentation with each release. It's always possible you'll discover something that's been there for years...
@Quentin wrote:
I read @Shmuel's answer and thought "symputn, what the heck is that?" After 5 minutes of googling, turns out it's only valid for SCL programs, not DATA step programs. But it's a good reminder to me to keep rereading the documentation with each release. It's always possible you'll discover something that's been there for years...
Or migrated from one area to another.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.