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;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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