BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
LL5
Pyrite | Level 9 LL5
Pyrite | Level 9

I am trying to print out an error message (with the red color) in the log if certain conditions are met inside a macro.

The first data step outside of a macro worked as intended, I am able to see the red color error message in the log.

However, I want to achieve the same within the macro, but putlog or %putlog does not work inside the macro, and %put does not print out the error message in the same way as the first data step did.

Can anyone share some insights on how to achieve this? Thanks

 

DATA T;
X = 3;
IF X = 3 THEN putlog "ERROR: this should not happen";
RUN;

%macro T;
DATA T;
X = 3;
%IF X = 3 %THEN %PUT "ERROR: this should not happen ";
RUN;
%mend;
%T;
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Why are you using %IF to evaluate DATA variables?

The text X=3 to the macro processor.

 

%macro T;
DATA T;
  X = 3;
  IF X = 3 THEN PUT "ERROR: this should not happen ";
RUN;
%mend;

%T;

If you want to compare the value of a MACRO variable:

 

%let x=3;
%if &x. = 3 %then %do;
   %put ERROR: This is an error message;
%end;

View solution in original post

4 REPLIES 4
WarrenKuhfeld
Ammonite | Level 13

Have you tried getting rid of the quotes with %PUT?

LL5
Pyrite | Level 9 LL5
Pyrite | Level 9

Yes, I realized it didn't work because I had quote around the text. Thanks for the tips! 

ballardw
Super User

Why are you using %IF to evaluate DATA variables?

The text X=3 to the macro processor.

 

%macro T;
DATA T;
  X = 3;
  IF X = 3 THEN PUT "ERROR: this should not happen ";
RUN;
%mend;

%T;

If you want to compare the value of a MACRO variable:

 

%let x=3;
%if &x. = 3 %then %do;
   %put ERROR: This is an error message;
%end;
LL5
Pyrite | Level 9 LL5
Pyrite | Level 9

@ballardw That's great. I know what the issue is - 1)I shouldn't have use quoted to wrap the error message inside the macro. 2)Also, I am not aware we can use %if %then %else outside of a macro, so this is a great discovery for me. 

Lastly, I used %if to evaluate data variable is because I used a bad example to ask this question and I realized it's a bad demonstration.

Thanks for your solution, that's amazing great! 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1606 views
  • 2 likes
  • 3 in conversation