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

With a data step, I can put a macro variable inside another one:

 

data _null_;
  call symput('b','&a');
run;

 

So &a is now inside b.

Check:

%let a=33;
%put &b;

==> 33
%let a=44;
%put &b;

==> 44

 

How can I do the same without a datastep, with pure macro programming?

So the code should look like:

 

%let b= ???(&a);

%let a=33;
%put &b;

%let a=44;
%put &b;

 

Note that I want to use &b without unquoting. With unquoting it is easy:

 

%let b=%nrstr(&a);
%let a=33;
%put --- %unquote(&b);
%let a=44;
%put --- %unquote(&b);

 

 

But that is not what I want.

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19
/* Here is a data-step free solution */

%let b=&&%unquote(a);
%let a=33; 
%put &b;
%let a=44; 
%put &b;


/* If you dislike the %UNQUOTE function and know the ASCII table, try this */

%let b=&&%sysfunc(byte(97));
%let a=33; 
%put &b;
%let a=44; 
%put &b;


/* Similar, but showing the "a" in the definition of b: */

%let b=&&%sysfunc(byte(%sysfunc(rank(a))));
%let a=33; 
%put &b;
%let a=44; 
%put &b;


/* Yet another option: */

%let b=&&%sysfunc(cat(a));
%let a=33;
%put &b;
%let a=44;
%put &b;

View solution in original post

7 REPLIES 7
Patrick
Opal | Level 21

I believe this magic is not possible as a function style macro.

 

When passing a value to a macro variable which contains an ampersand you must quote the value however you code it.

 

You could of course wrap your data step into a macro and then call this macro with two parameters.

%macro not_so_magic(l,r);
  %global &l;
  data _null_; 
    call symput("&l",'&'||"&r"); 
  run;
%mend;

%not_so_magic(b,a)
%let a=33; 
%put &=b;
%let a=44; 
%put &=b;

 

FreelanceReinh
Jade | Level 19

What about this?

%let c=%sysfunc(dosubl(data _null_; call symput('b','&a'); run;));
%let a=33; 
%put &b;
%let a=44; 
%put &b;

Ok, it says "%let c=...", not "%let b=...", but what's in a name, after all? Smiley Happy

JMS
Obsidian | Level 7 JMS
Obsidian | Level 7

Its not that I need the solution for any project.

I just want to learn about macro and how I can put &a into b.

 

FreelanceReinh
Jade | Level 19
/* Here is a data-step free solution */

%let b=&&%unquote(a);
%let a=33; 
%put &b;
%let a=44; 
%put &b;


/* If you dislike the %UNQUOTE function and know the ASCII table, try this */

%let b=&&%sysfunc(byte(97));
%let a=33; 
%put &b;
%let a=44; 
%put &b;


/* Similar, but showing the "a" in the definition of b: */

%let b=&&%sysfunc(byte(%sysfunc(rank(a))));
%let a=33; 
%put &b;
%let a=44; 
%put &b;


/* Yet another option: */

%let b=&&%sysfunc(cat(a));
%let a=33;
%put &b;
%let a=44;
%put &b;
Patrick
Opal | Level 21

@FreelanceReinh

Nice! The doubled ampersand allows you to pass in the ampersand without quoting and after that anything will do that allows to pass in the 'a' without adding a quote to the ampersand but still in a way that the tokenizer doesn't treat it as a macro variable.

 

Here another option

%let b=&&%left(a);
%let a=33; 
%put b has value: &b;
%let a=44; 
%put b has value: &b;
JMS
Obsidian | Level 7 JMS
Obsidian | Level 7

BTW, if you want to look what is "really" in &b, use %superq(b)

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

The question I wouold ask is why?  I find that 99% of tasks can be done in Base SAS.  Macro is a utility language to help in generate repeating code and such like, its just a text generator at the end of the day.  If I come across a program which has more than one ampersand or percentage in a line, I just delete the program and start again.  Have yet to see a valid reason for resorting to macro when re-shaping of data and array/aggregates don't do the same job.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 7 replies
  • 2242 views
  • 6 likes
  • 4 in conversation