DATA Step, Macro, Functions and more

Quote the quotes

Accepted Solution Solved
Reply
Respected Advisor
Posts: 4,173
Accepted Solution

Quote the quotes

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


Accepted Solutions
Solution
‎02-25-2015 07:54 AM
Respected Advisor
Posts: 4,173

Re: Quote the quotes

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;

View solution in original post


All Replies
Solution
‎02-25-2015 07:54 AM
Respected Advisor
Posts: 4,173

Re: Quote the quotes

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;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 1 reply
  • 195 views
  • 1 like
  • 1 in conversation