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;
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;
Have you tried getting rid of the quotes with %PUT?
Yes, I realized it didn't work because I had quote around the text. Thanks for the tips!
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;
@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!
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!
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.
Ready to level-up your skills? Choose your own adventure.