sasrtfproc-report

How can you get titles and footnotes on the table of contents page in ods rtf output?


I would like an RTF file with a table of contents, but I also want the titles and footnotes on the same page as the table of contents. Here is code that will produce an RTF file with a table of contents that only has one TOC line per proc report table:

data testdata;
  input letters $ numbers;
  cards;
A 1
B 2
C 3
;
run;

data testdata;
  set testdata;
  dummy=1;
run;

ods rtf FILE="test.rtf" startpage=no style=analysis CONTENTS=YES toc_data;

ods escapechar="^";
title1 j=l "This title should be on every page" j=r "Page ^{pageof}";
title2 j=l "(even the first one)";
footnote1 "This footnote should be on every page, too";

ods rtf text="{\pard\page\par}";
ods proclabel 'Test Data';
proc report nowd data=testdata contents='';
  column dummy ('Test Data' letters numbers);
  define dummy / group noprint;
  define letters / "Letters";
  define numbers / "Numbers";
  break before dummy / contents='' page;
run;

ods rtf close;

How can I make it so that the titles and footnote appear on the first page with the table of contents as well as the rest of the document?


Solution

  • This is a hack, but it's the only way I could find to make it work. The crux of it is that we are just going to alter the RTF file that is output by SAS using some SAS programming. The key is here where I read in the file, pull out the first header and footer information, then insert a copy at the beginning of the document and output to a separate file:

    data edit hf;
      infile "test.rtf" dlm='09'x dsd lrecl=32767 missover;
      format var $200.;
      input var $;
      output edit;
      retain head fhead ffoot 0;
      if index(var,'{\header')>0 and fhead=0 then head = 1;
      if head = 1 and fhead=0 then output hf;
      if index(var,'{\footer')>0 then ffoot=1;
      if index(var,'\pard}}\trowd\trkeep\trql')>0 and ffoot=1 then fhead=1;
      keep var;
    run;
    
    data edit1 edit2;
      set edit;
      retain start 0;
      if index(var,'\widowctrl\')>0 then start=1;
      if start=0 then output edit1;
      else output edit2;
      keep var;
    run;
    
    data out;
      set edit1 hf edit2;
    run;
    
    data _null_;
      set edit1 hf edit2;
      file 'test1.rtf';
      put var;
    run;
    

    Of course, there was also the date and page number that SAS puts in there automatically that I didn't want, so I updated that option before creating the original RTF file. Here is all the code together in case it helps someone in the future:

    data testdata;
      input letters $ numbers;
      cards;
    A 1
    B 2
    C 3
    ;
    run;
    
    data testdata;
      set testdata;
      dummy=1;
    run;
    
    options nodate nonumber;
    ods rtf FILE="test.rtf" startpage=no style=analysis CONTENTS=YES toc_data;
    
    ods escapechar="^";
    title1 j=l "This title should be on every page" j=r "Page ^{pageof}";
    title2 j=l "(even the first one)";
    footnote1 "This footnote should be on every page, too";
    
    ods rtf text="{\pard\page\par}";
    ods proclabel 'Test Data';
    proc report nowd data=testdata contents='';
      column dummy ('Test Data' letters numbers);
      define dummy / group noprint;
      define letters / "Letters";
      define numbers / "Numbers";
      break before dummy / contents='' page;
    run;
    
    ods rtf close;
    
    data edit hf;
      infile "test.rtf" dlm='09'x dsd lrecl=32767 missover;
      format var $200.;
      input var $;
      output edit;
      retain head fhead ffoot 0;
      if index(var,'{\header')>0 and fhead=0 then head = 1;
      if head = 1 and fhead=0 then output hf;
      if index(var,'{\footer')>0 then ffoot=1;
      if index(var,'\pard}}\trowd\trkeep\trql')>0 and ffoot=1 then fhead=1;
      keep var;
    run;
    
    data edit1 edit2;
      set edit;
      retain start 0;
      if index(var,'\widowctrl\')>0 then start=1;
      if start=0 then output edit1;
      else output edit2;
      keep var;
    run;
    
    data out;
      set edit1 hf edit2;
    run;
    
    data _null_;
      set edit1 hf edit2;
      file 'test1.rtf';
      put var;
    run;