I am attempting to concatenate various values to a string: macro variables, number variables, _N_, plain text, even an observation from a dataset. Then pass the sting to a macro.
%macro err_msg(param_msg);
%put &=param_msg;
%mend err_msg;
data _null_;
%let TargetYear="2018";
number = 27;
msg = cat('Note: The year is ', &TargetYear, ', number is ', number);
put msg $char.;
%err_msg(msg);
run;
But this is what I get in the log
24 %let err_msg_init = 1;
25 %let RedTitle= font='Consolas' color=red height=14pt;
26
27 %macro err_msg(param_msg);
28 %put &=param_msg;
29 %mend err_msg;
30 data _null_;
31 %let TargetYear="2018";
32 number = 27;
33 msg = cat('Note: The year is ', &TargetYear, ', number is ', number);
34 put msg $char.;
35 %err_msg(msg);
PARAM_MSG=msg
36 run;
Note: The year is 2018, number is 27
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds
String in blue seems to concat OK, but shown in red, the macro sees "msg"
Thanks in advance.
You are missing semi colon here
number = 27
should be
number = 27;
I saw that, thanks a lot fixed, still have the macro not seeing the string.
missing ;
30 data _null_; 31 %let TargetYear="2018"; 32 number = 27 <= should have ; 33 msg = cat('Note: The year is ', &TargetYear, ', number is ', number);
Thanks, fixed and I only have the macro not seeing the string, only the word "msg", shown in red.
Seems to be taking msg as just a string instead of passing the actual variable in
%err_msg(msg);
You need call execute or resolve
Lots of examples available online but pretty boring syntax though dealing with concatenation of literals lol
I like resolve 🙂
%macro err_msg(param_msg);
param_msg is ¶m_msg;
%mend err_msg;
data _null;
%let TargetYear="2018";
number = 27;
msg = cat('Note: The year is ', &TargetYear, ' number is ', number);
x=resolve('%err_msg'||'('||msg||')');
put +5 x=;
run;
Thanks a lot will chew on this, a little slow as newbie.
@Espresso My apologies for the laziness on my part. Here you go:
%macro err_msg(param_msg);
%put &=param_msg;
%mend err_msg;
data _null_;
%let TargetYear="2018";
number = 27;
msg = cat('Note: The year is ', &TargetYear, ' number is ', number);
call execute('%err_msg'||'('||left(msg)||')');
run;
Given what you have gone through so far, here are more important lessons that are more appropriate for a relative newcomer to SAS.
First, macro language statements are not part of a DATA step. Macro language does not recognize DATA step variables. Thus your observation is correct: %err_msg(msg) is treating "msg" as three letters, not as a reference to a variable.
Second, the DATA step is able to transfer a DATA step value to a macro variable. It contains special tools for this purpose, such as:
call symputx('message', msg);
This transfers the value of the DATA step variable MSG to a macro variable named MESSAGE. If you were to include that line in your DATA step, you could add:
%put &=message;
You would have to add this AFTER the DATA step completes running, meaning after the RUN statement that ends the DATA step.
Perhaps macro is not the way to go then.
OK, this question is a subset of the bigger purpose I need to accomplish, so I will start a new thread, than you all.
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!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.