https://github.com/scottbass/SAS/blob/master/Macro/squote.sas
P.S.: Your particular example is poor - you'd just replace the single quotes with double quotes.
I'll run with your subject line and not your example.
Why do you need single quotes? Please show us the (complete) desired end result.
Does that answer your question?
%let name=jhonson;
Data ex1;
Myname = compress(catx("","'","&name","'")) ;
Run;
you cannot resolve the &name or any macro variable with single quote, we need to use the double quotes to resolve the macro variables.
Macro expressions in single quotes are not resolved. There are several ways to get you the result you want, one is to use double quotes as @ScottBass suggested:
Data ex1;
Myname="&name" ;
Run;
Another is to use the SYMGET function to get the value:
Data ex1;
Myname=symget('NAME');
Run;
If you absolutely need to get the macro value in single quotes, the easiest is probably to use the QUOTE function:
Data ex1;
Myname=%sysfunc(quote(&name,%str(%'))) ;
Run;
I just borrowed (stole?) Tom's original work for my macro.
If you add outer double quotes, the macro variable reference will be resolved despite the inner single quotes:
data ex1;
Myname="'&name'";
run;
This assumes that you want the single quotes to be part of the string stored in the character variable.
At this point, the answers have been given to the question asked; but I think the real question that needs an answer is why do you want to resolve a macro variable in single quotes in the first place? This seems like a misunderstanding and could have been easily solved if only you told us what the real goal of the program and explained where you are and where you want to go, rather than you want to resolve a macro in single quotes.
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!
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.
Ready to level-up your skills? Choose your own adventure.