DATA Step, Macro, Functions and more

assign value to macro

Accepted Solution Solved
Reply
Contributor
Posts: 32
Accepted Solution

assign value to macro

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

 

 


Accepted Solutions
Solution
2 weeks ago
Trusted Advisor
Posts: 1,831

Re: assign value to macro

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


All Replies
Valued Guide
Posts: 516

Re: assign value to macro

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.

Solution
2 weeks ago
Trusted Advisor
Posts: 1,831

Re: assign value to macro

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.

PROC Star
Posts: 1,449

Re: assign value to macro

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

Super User
Posts: 13,304

Re: assign value to macro


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

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 145 views
  • 2 likes
  • 5 in conversation