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

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_&macro_variable

 

which would resolve to a variable called case_ABC123 which I can then in turn assign values to.

 

Regards,

Mark

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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.

View solution in original post

4 REPLIES 4
andreas_lds
Jade | Level 19

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.

Shmuel
Garnet | Level 18

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.

Quentin
Super User

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

ballardw
Super User

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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 4 replies
  • 2122 views
  • 2 likes
  • 5 in conversation