BookmarkSubscribeRSS Feed
Yoko
Obsidian | Level 7

When I asked a question about saving an rtf file using program name (https://communities.sas.com/t5/Base-SAS-Programming/Using-program-name-as-ods-rtf-file-name/m-p/4103...), I thought I found a solution. 

 

data _null_;
  tmp="%sysfunc(GetOption(SYSIN))";
  call symput('fname',scan(tmp,2,"]"));
run;

ods rtf file="&fname.";
...

But, later, I found that this program replaced the content of the current program with results in an rtf format.  That is, when I click my program (e.g., my_program.sas), it opens as a rtf file of the results. 

 

Is there a way to run my program (e.g., my_program.sas) and automatically create a result file as my_program.rtf without typing 'my_program'?

 

Thank you!

(I'll check the program and results more carefully this time!) 

 

Yoko

6 REPLIES 6
ballardw
Super User

Perhaps you need to examine the value that &fname has and then replace the likely .SAS extension with .RTF .

This would most likely be easiest in the data _null_ step by modifying the TMP variable prior to the Call Symput. You may want to consider using call symputx as well. Symputx strips some blank characters that occasionally may cause problems.

Yoko
Obsidian | Level 7

Hello, 

 

Thank you for your post (and sorry not to respond to your post sooner). 

 

When I replace 

ods rtf file="&fname.";

with 

ods rtf file="&fname.rtf.";

 A file 'temp.sasrtf' was created.  This is my result file. 

Yes, the program name is 'temp.sas'. 

It worked somehow even though extension looks strange!  I wish I could just have 'temp.rtf'....  

 

Yoko

 

 

Tom
Super User Tom
Super User

@Yoko wrote:

Hello, 

 

Thank you for your post (and sorry not to respond to your post sooner). 

 

When I replace 

ods rtf file="&fname.";

with 

ods rtf file="&fname.rtf.";

 A file 'temp.sasrtf' was created.  This is my result file. 

Yes, the program name is 'temp.sas'. 

It worked somehow even though extension looks strange!  I wish I could just have 'temp.rtf'....  

 

Yoko 


The reason the file was created with an extension of 'sasrtf' instead of 'sas.rtf' is that the SAS macro processor used the period after &FNAME as an indication of where the name of the macro variable ended. Without the period SAS would look for a macro variable named FNAMERTF.  So if you want to get an actual period in front of rtf you need to have two of them. One to mark the end of the macro variable reference and the other to become part of the file name.

ods rtf file="&fname..rtf.";
Yoko
Obsidian | Level 7

Hello, 

 

I tried: ods rtf file="&fname..rtf.";

And I got this error message:  ERROR: Invalid open mode.

An rtf file was not created. 

 

Strangely, the program went through without an obvious error message, but when I checked the log, I found errors..

 

Yoko

 

 

Tom
Super User Tom
Super User

Where did you get that message?

Are you trying to write a directory that is read only?  Or that already has a file with that name that you cannot delete or overwrite?

Are you running some interactive environment like SAS Display Manger that might try to let you preview RTF files that you created?  If so then perhaps it is too dumb to know what to do with a file with two periods in its name?  'test.sas.rtf'  That is why I normally try to replace the .sas with the new extension rather than just appending '.rtf'.

 

The way to test is to run it once with the filename you use hardcoded and see if you can create the error or fix it by using different filenames.  Once you know what works then adjust your code to handle it.

 

Here is some simple code that will take value of macro variable FNAME and replace '.sas' suffix with '.rtf' suffix.  If there is no '.sas' suffix then it just adds the '.rtf' to the end.

data _null_;
  length fname $300;
  fname=symget('fname');
  if upcase(scan(fname,-1,'.'))='SAS' then fname=substr(fname,1,length(fname)-4);
  fname =cats(fname,'.rtf');
  call symputx('fname',fname);
run;
Yoko
Obsidian | Level 7

Hello, 

 

(Sorry for not replying to you sooner)

I got the message 'ERROR: Invalid open mode' right after this line: ods rtf file="&fname..rtf.";

 

I tried your code you posted last time. 

I did not get any error or warning messages.  But, after the code, I saw this: 

NOTE: Invalid argument to function symget at linne...

fname = .rtf _ERROR_=1 _N_=1

 

Yoko

 

 

 

 

 

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 1049 views
  • 1 like
  • 3 in conversation