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

The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
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.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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