DATA Step, Macro, Functions and more

Using program name as ods rtf file name: Revisited

Reply
Contributor
Posts: 22

Using program name as ods rtf file name: Revisited

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

Super User
Posts: 11,343

Re: Using program name as ods rtf file name: Revisited

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.

Contributor
Posts: 22

Re: Using program name as ods rtf file name: Revisited

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

 

 

Super User
Super User
Posts: 7,076

Re: Using program name as ods rtf file name: Revisited


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.";
Contributor
Posts: 22

Re: Using program name as ods rtf file name: Revisited

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

 

 

Super User
Super User
Posts: 7,076

Re: Using program name as ods rtf file name: Revisited

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;
Ask a Question
Discussion stats
  • 5 replies
  • 77 views
  • 1 like
  • 3 in conversation