Search code examples
sasrtfsas-ods

SAS ODS RTF - Best option to embed Title/Footnote into Table


Looking for advice to embed title/footnote as part of the table (see below - making it easier to copy & paste into the different document)

enter image description here

Options explored so far

1) PROC REPORT - COMPUTE PAGE BEFORE (COMPUTE doesn't support justification option and did not find any reliable option to right-align "page x of y" text in title1 e.g. calculating and inserting BLANK space. In addition, I have a need to center align the title)

2) ODS RTF - BODYTITLE and BODYTITLE_AUX option (displays title/footnote as part of the body but not exactly as part of the table - not easy to select as one object)


Solution

  • The SAS ODS inline styling directive ^{PAGEOF} will produce Page x of y output in the output file. Since the output is a Word field you might need to Ctrl-A, F9 to compute the field values when the document is opened.

    The RTF destination renders TITLE and FOOTNOTE in the header and footer part of the documents, so tricks are needed to produce per-table 'titles' and 'footers'. From my perspective as a document reader, the best place for PAGEOF would be in the header section and not as part of a table header.

    ods escapechar = '^';
    title justify=right '^{PAGEOF}';  
    

    ODS TEXT= can be used to add paragraphs before and after a Proc TABULATE that does statistical reporting. ODS TEXT= will process inline ODS formatting directives (including PAGEOF). See SAS® 9.4 Output Delivery System: User’s Guide, Fifth Edition, ODS ESCAPECHAR Statement for more info.

    ods text='
    Narrative before tabular output via ODS TEXT=
    ^{NEWLINE} Inline styling directives will be processed.
    ^{NEWLINE} ^{STYLE [color=green]Mix and match}
    ^{NEWLINE} Let''s see why.
    ';
    
    ods text='Here you are at ^{STYLE ^{PAGEOF}}';
    * This might require Word field computation or print preview to get proper numbers;
    

    Depending on the destination STYLE=, such as ods rtf style=plateau … ODS TEXT might have some outlining or other styling artifacts.

    If you are rtf hardcore, ODS TEXT= can also inject rtf codes directly into the destination stream to produce any rtf possible production. In the following example:

    • legacy style function ^S{attr-name=attr-value} is used to force the text container to be 100% of the page width.
    • current style function ^{RAW function is used to introduce raw rtf coding. Each { of the actual rtf is introduced using {RAW. Note how the RAWs can be (and are) nested.

    ods text = '^S={outputwidth=100% just=c} ^{RAW \rtf1\ansi\deff0^{RAW \fonttbl^{RAW \f0 Arial;}} 
    \qc\f0\fs20\i\b
     This output created with ODS TEXT=\line
     Injecting raw RTF coding\line
     Such as ^{RAW \cf11\ul colored and underlined}\line
     Not for the casual coder.
    }'; 
    

    Some procedures, such as Proc PRINT have style options such as style(table)=[ … ] in which a pretext= and posttext= can be specified, and such texts will be rendered before and after the rtf table -- the texts are not part of the table and would not be 'picked up' in a single click of Word's 'table select' icon (Table select icon). Also, unfortunately, the pretext= and posttext= values are not processed for ODS styling directives. However, the values can be raw rtf!

    PRETEXT demonstrating inline styling is not honored:

    proc print
      data=sashelp.class (obs=3)
      noobs
      style(table)=[
       pretext='^{STYLE [color=Red]Above 1 ^{NEWLINE}Above 2 - Pretext= is unprocessed ODS directives}' 
       posttext='^{STYLE [color=Green] Below}'
      ]
    ;
    run;
    

    output pic

    PRETEXT demonstrating as raw rtf passed through (when first character is {)

    proc print 
      data=sashelp.class (obs=3)
      noobs
      style(table)=[
        pretext='{\rtf1\ansi\deff0{\fonttbl{\f0 Arial;}}
    \qc\f0\fs20\i\b This output created with SAS PRETEXT=
    \line Injecting raw RTF coding
    \line Not for the casual coder.
    }'
        posttext='{\rtf1\ansi\deff0{\fonttbl{\f0 Arial;}}
    \qc\f0\fs20\i\b This output created with SAS POSTTEXT=
    \line Injecting raw RTF coding
    \line Not for the casual coder.
    }'
      ]
    ;
    run;
    

    output pic

    Proc TABULATE does not have a style(table) option, but the TABLE statement does have options:

    • / CAPTION= (rendered when OPTION ACCESSIBLETABLE; active)
      • Note: Caption value is rendered in the page dimension container, so the caption value be overwritten if your TABLE statement has a page dimension in it's crossings.
    • / STYLE=[PRETEXT='...' POSTTEXT='...']
      • Same caveats as mentioned earlier

    TABULATE does not:

    • have any feature that will let you annotate a column header with a row statistic (in this case your (N=###) as part of the category value). A precomputation step that summarizes the crossings to be tabulated will allow you to place a statistic there.
    • provide any mechanism for inserting a blank or label row that spans the table (such as the LINE statement in Proc REPORT)

    Consider this tabulate example with accessibletable on for some medical data. Some of the variables are for:

    • categorical demographics (such as sex),
    • continuous measures (such as age cost),
    • binary flags about state.
    data have;
      call streaminit(123);
    
      do _n_ = 1 to 1e3 - 300 + rand('uniform',600);
        patientId + 1;
    
        category = ceil(rand('uniform',4));
    
        age = 69 + floor(rand('uniform',20));
        cost = 500 + floor(rand('uniform',250));
    
        length sex $1;
        sex = substr('MF', 1+rand('uniform',2));
    
        array flags flag1-flag3; * some flags asserting some medical state is present;
        do over flags; flags = rand('uniform', 4) < _i_; end;
    
        output;
      end;
    
      label age = 'Age' cost = 'Cost' sex = 'Sex';
    run;
    
    * Precomputation step;
    * Use SQL to compute the N= crossings and make that part of a
    * new variable that will be in the tabulation column dimension;
    
    proc sql;
      create table for_tabulate as
      select 
        *
        , catx(' ', 'Category', category, '( n =', count(*), ')') 
          as category_counted_columnlabel
      from have
      group by category
      ;
    quit;
    

    Tabulation report

    options accessibletable;
    
    proc tabulate data=for_tabulate ;
    
      class 
        category_counted_columnlabel 
        sex
      /
        missing style=[fontsize=18pt]
      ;
    
      var age cost flag1-flag3;
    
      table
    
        /* row dimension */
        age * ( mean std min max median n*f=8.) * [style=[cellwidth=0.75in]]
        N='Sex' * sex
        cost * ( mean std min max median n*f=8. ) 
        (flag1 - flag3) * mean = '%' * f=percent5.1
    
        ,
    
        /* column dimension */
        category_counted_columnlabel = ''
    
        /
    
        /* options */
        nocellmerge
        caption = "This is caption text is ^{STYLE [color=green]from Mars}^{NEWLINE}Next line"
      ;
    
    run;
    

    enter image description here

    For SAS versions before 9.4M6 you can add a page dimension variable (whose value is inlined ODS stylized text to be the caption) to the output of the precomputation step and specify that variable in the table statement. Something like

    ,    '^{NEWLINE}title1'  /* SQL step */
      || '^{NEWLINE}title2'
      || '^{NEWLINE}title3'
      || '^{NEWLINE}title4'
      as pagedim_title
    

    and

    /* tabulate step */
    class pagedim_title / style = [background=lightgray fontfamily=Arial textalign=right];
    table pagedim_title, …, category_counted_columnlabel = '' … ;