Why doesn't this work?
/*******************/
data temp;
do id = 1 to 10;
x=ranuni(134);
y=ranuni(135);
output;
end;
run;
ods rtf file="<your directory>\test.rtf";
%macro m1 ();
%let mytitle=This is my title;
%let xvalue=;
data _null_;
set temp;
call symputx ('xvalue',x);
run;
%let yvalue=999;
%if (&yvalue ne ) %then ods rtf text="Xvalue is %sysfunc(round(&xvalue,.01)).";
title "&mytitle";
proc sgplot data=temp;
scatter x=x y=y;
run;
title ;
%mend m1;
%m1;
ods rtf close;
/*********************/
When I execute this code, I get the following error message:
TIA
The macro code is in red:
%if (&yvalue ne ) %then ods rtf text="Xvalue is %sysfunc(round(&xvalue,.01))." ;
As you can see you haven't closed the ODS statement.
You can add a %do block as shown, or another proper way would be:
%if (&yvalue ne ) %then ods rtf text="Xvalue is %sysfunc(round(&xvalue,.01))."%str(;) ;
or just as good (and you'll get a semicolon even if the test fails):
%if (&yvalue ne ) %then ods rtf text="Xvalue is %sysfunc(round(&xvalue,.01)). ; ;
Use a "DO; END" block on that if statement.
data temp;
do id = 1 to 10;
x=ranuni(134);
y=ranuni(135);
output;
end;
run;
ods rtf file="<your directory>\test.rtf";
%macro m1 ();
%let mytitle=This is my title;
%let xvalue=;
data _null_;
set temp;
call symputx ('xvalue',x);
run;
%let yvalue=999;
%if (&yvalue ne ) %then %do;
ods rtf text="Xvalue is %sysfunc(round(&xvalue,.01)).";
%end;
title "&mytitle";
proc sgplot data=temp;
scatter x=x y=y;
run;
title ;
%mend m1;
%m1;
ods rtf close;
Hi @cminard You are missing a semi colon to end the ODS RTF for the reason the semi colon present acts as step boundary rather for the %IF macro statement. You would need another semi colon, however the inner semi colon would have to be quoted aka masked to be treated as text using %STR funciton. So-
%if (&yvalue ne ) %then ods rtf text="Xvalue is %sysfunc(round(&xvalue,.01))."%str(;) ;
I would recommend moving all macro definitions outside of an ODS destination block (ODS destination/ ods destination close ). Partially it makes code easier to follow but may also address your spefic issue. Additionally to test just the output part you don't end up recompiling the macro multiple times (efficiency) and may be easier test.
Dummy code to illustrate what I mean.
%macro m1(); <code in macro> %mend m1; ods rtf file ="path\file.rtf"; %m1; ods rtf close;
Debug macro behavior by setting OPTIONS MPRINT; before execution. The log will usually have more details of what the macro compiler actually generated.
Use Options NOMPRINT; to turn of the details.
Thank you all. Yes, I am missing a semi-colon to close that %if statement. Thanks for all other suggestions as well.
The macro code is in red:
%if (&yvalue ne ) %then ods rtf text="Xvalue is %sysfunc(round(&xvalue,.01))." ;
As you can see you haven't closed the ODS statement.
You can add a %do block as shown, or another proper way would be:
%if (&yvalue ne ) %then ods rtf text="Xvalue is %sysfunc(round(&xvalue,.01))."%str(;) ;
or just as good (and you'll get a semicolon even if the test fails):
%if (&yvalue ne ) %then ods rtf text="Xvalue is %sysfunc(round(&xvalue,.01)). ; ;
Intended to mark this one as the solution.
You can change
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.