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?
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
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" ';
Train your users on how to pass in the messages so that they don't generate errors.
True. That's always the last resort though.. 🙂
This %SQUOTE() macro can help.
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;
Good ideas but it's probably html that will be passed on in many cases.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.