BookmarkSubscribeRSS Feed
ChrisNZ
Tourmaline | Level 20

Hi,

I need to dump a macro variable as is in a put statement. I can't find a way to do this cleanly.

 

 

%macro test(msg);
  data _null_; putlog '1 ' &msg. ;                 run;
  data _null_; putlog '2 ' %unquote(%superq(msg)); run;
  data _null_; putlog '3 ' %superq(msg) ;          run;
%mend;

%test( "a%str(&)b" / "bbb" );

The first two data steps generate  WARNING: Apparent symbolic reference B not resolved. 

 

The third one fails to recognize the macro variable as valid sas code.

Any idea?

8 REPLIES 8
Tom
Super User Tom
Super User

Your code is referencing some undefined macro variable A instead of the using MSG, the actual name of the parameter of the macro.

 

But it really depends on WHAT you are expecting to print. If you want to print the actual value of the macro variable without the macro processor evaluating it then pull it into a character variable using the SYMGET() function. If you want the data step to interpret the value as two strings separated by the / new line command for a PUT statement then use single quotes to prevent the macro processor from evaluating the strings.

647  %macro test(msg);
648    data _null_; putlog '1 ' &msg ;                 run;
649    data _null_; length msg $200; msg=symget('msg'); put '4 ' msg ; run;
650  %mend;
651
652  options mprint;
653  %test( 'a&b' / "bbb" );
MPRINT(TEST):   data _null_;
MPRINT(TEST):   putlog '1 ' 'a&b' / "bbb" ;
MPRINT(TEST):   run;

1 a&b
bbb
NOTE: DATA statement used (Total process time):
      real time           0.16 seconds
      cpu time            0.00 seconds


MPRINT(TEST):   data _null_;
MPRINT(TEST):   length msg $200;
MPRINT(TEST):   msg=symget('msg');
MPRINT(TEST):   put '4 ' msg ;
MPRINT(TEST):   run;

4 'a&b' / "bbb"
NOTE: DATA statement used (Total process time):
      real time           0.14 seconds
      cpu time            0.00 seconds

 

ChrisNZ
Tourmaline | Level 20

Hi @Tom

 

Your code is referencing some undefined macro variable A 

Fixed, sorry about that.

 

I have no control over what values are sent.

Thinking about it over lunch, this was a silly question.

If the parameter value creates code that generates messages, there is nothing I can do about it. I just have to execute the code with whatever is sent. Running the put statement is the purpose here, so I must run it.

 

The text is going to an email message.

The only way I could avoid it is if there existed an !EM_MESSAGE! directive, such as as:

 put '!EM_MESSAGE!   "a%str(&)b" / "bbb"  ';

 

 

Tom
Super User Tom
Super User

Train your users on how to pass in the messages so that they don't generate errors.

Tom
Super User Tom
Super User

You could make your messaging macro smarter so that users are not passing in code, but data.

Perhaps something like:

%putmsg(msg1,msg2,msg3,msg4,msg5);
length msg $200;
do i=1 to 5 ;
  msg=symget(cats('msg',i));
  if msg ne ' ' then put msg ;
end;
%mend;
ChrisNZ
Tourmaline | Level 20

Good ideas but it's probably html that will be passed on in many cases.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 8 replies
  • 751 views
  • 2 likes
  • 2 in conversation