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