Hi guys
I'm trying to implement a SAS macro which generates a data step attrib statement. The problem comes from a real life scenario where I want to automate table creation based on SAS DI Studio 4.9 generated code.
The "%let have" statement is a representation of the auto-generated code bit so this statement is a given and can't be modified.
The %let statement will be auto-generated and no modification is possible.
%let have = %nrquote(Peter & Paul%'s label);
I can implement whatever macro code I want. Below does not work 😞 so the code posted is where I am right now and got stuck.
%macro codegen;
%let label=%sysfunc(transtrn(&have,%nrquote(%'),%nrquote(%'%')));
data want;
attrib var length=8 label=%nrquote(%')%str(&label)%nrquote(%');
stop;
run;
%mend;
%codegen;
And here what I would like the macro to return. This code works if executed on its own.
data want;
attrib var length=8 label='Peter & Paul''s label';
stop;
run;
My environment is SAS9.4 M2 under RHEL
Thanks
Patrick
O.K. - this was now ridiculously hard to work out. But at least I believe I've got it now. I normally don't mark my own answer as "correct" but this time I feel I deserve a treat.
%let have = %nrquote(Peter & Paul%'s label);
%macro codegen;
%let label=%str(%')%qsysfunc(transtrn(&have,%str(%'),%str(%'%')))%str(%');
data want;
attrib var length=8 label=%unquote(&label);
stop;
run;
%mend;
%codegen;
Result:
MPRINT(CODEGEN): data want;
MPRINT(CODEGEN): attrib var length=8 label='Peter & Paul''s label';
MPRINT(CODEGEN): stop;
MPRINT(CODEGEN): run;
O.K. - this was now ridiculously hard to work out. But at least I believe I've got it now. I normally don't mark my own answer as "correct" but this time I feel I deserve a treat.
%let have = %nrquote(Peter & Paul%'s label);
%macro codegen;
%let label=%str(%')%qsysfunc(transtrn(&have,%str(%'),%str(%'%')))%str(%');
data want;
attrib var length=8 label=%unquote(&label);
stop;
run;
%mend;
%codegen;
Result:
MPRINT(CODEGEN): data want;
MPRINT(CODEGEN): attrib var length=8 label='Peter & Paul''s label';
MPRINT(CODEGEN): stop;
MPRINT(CODEGEN): run;
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.
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.