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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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

1 REPLY 1
Patrick
Opal | Level 21

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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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