BookmarkSubscribeRSS Feed
Espresso
Obsidian | Level 7

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.

11 REPLIES 11
novinosrin
Tourmaline | Level 20

You are missing semi colon here

number = 27

should be

number = 27;

 

Espresso
Obsidian | Level 7

I saw that, thanks a lot fixed, still have the macro not seeing the string.

ballardw
Super User

missing ;

 

30         data _null_;
31         	%let TargetYear="2018";
32         	number = 27         <= should have ;
33         	msg = cat('Note: The year is ', &TargetYear, ', number is ', number);
Espresso
Obsidian | Level 7

Thanks, fixed and I only have the macro not seeing the string, only the word "msg", shown in red.

Espresso
Obsidian | Level 7

Seems to be taking msg as just a string instead of passing the actual variable in 

%err_msg(msg);
novinosrin
Tourmaline | Level 20

You need call execute or resolve 

 

Lots of examples available online but pretty boring syntax though dealing with concatenation of literals lol

novinosrin
Tourmaline | Level 20

I like resolve 🙂

 


%macro err_msg(param_msg);
	param_msg is  &param_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;
Espresso
Obsidian | Level 7

Thanks a lot will chew on this, a little slow as newbie.

novinosrin
Tourmaline | Level 20

@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;
Astounding
PROC Star

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.

Espresso
Obsidian | Level 7

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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 11 replies
  • 1326 views
  • 0 likes
  • 4 in conversation