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

BASUG is hosting free webinars Next up: Mike Sale presenting Data Warehousing with SAS April 10 at noon ET. Register now at the Boston Area SAS Users Group event page: 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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 1253 views
  • 2 likes
  • 5 in conversation