BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cminard
Obsidian | Level 7

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:

cminard_1-1602707296528.png

 

TIA

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

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)).  ;

 

 

 

 

 

View solution in original post

7 REPLIES 7
CurtisMackWSIPP
Lapis Lazuli | Level 10

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;
novinosrin
Tourmaline | Level 20

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(;) ;
ballardw
Super User

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.

 

cminard
Obsidian | Level 7

Thank you all. Yes, I am missing a semi-colon to close that %if statement. Thanks for all other suggestions as well.

ChrisNZ
Tourmaline | Level 20

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)).  ;

 

 

 

 

 

cminard
Obsidian | Level 7

Intended to mark this one as the solution.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 1064 views
  • 3 likes
  • 5 in conversation