Generally speaking, there are three kinds of output in the pharmaceutical industry: listing, rtf and PDF. Among them, RTF output is used most widely. First of all, RTF is readable across many programs (including Microsoft Word) and platforms. Second, Microsoft Word offers reviewers (usually statisticians) powerful tools to track changes or insert comments. Finally, rtf code allows more flexibility to customize rtf output.

What is RTF (Rich Text Format)?

Rich Text Format (RTF) is a file format that lets you exchange text files between different operating systems and different software applications.

It consists of unformatted text, control words, control symbols and groups. Unformatted text is what will be displayed and what you can see once an rtf file is opened in a RTF reader like Microsoft Word. Control words and control symbols serve as formatting command which can tell RTF reader how to manage and display RTF document. Groups such as bookmark or comment consist of text and control words or symbols which are enclosed in braces. A group specifies text affected by the group and different attributes of that text.

Structurally speaking, it is made of two parts – header and document area. RFT header are instructions on how the file will be rendered by viewing application. It let RTF reader knows how to interpret control commands, to format and display document area.

For detailed information, you can read RTF specification on your own.

What is RTF specification?

A specification is something similar to a guideline which can show you how to create a RTF file. It explains syntax for RTF code. Here  is where you can download the latest version.

How to create a RTF file?

With the specification, we can even create a simple RTF file from scratch. The most widely used software is notepad and Microsoft Word. First of all, write RTF code in notepad and save it as .txt or .rtf file. Next, open the file in Microsoft Word.

Example 1 – how to create a paragraph

Following shows you almost the easiest rtf file in the world. RTF header consists of “\rtf1\ansi” and the rest RTF code belongs to document area. Only “This is 1st RTF sample. “ is unformatted text. Others are control words. As you can see that, only unformatted text can be viewed when opening in RTF reader like Microsoft Word.

Example 2 – how to create a table

It is well known that we need to develop a lot of tables in the pharmaceutical industry. Even listing is in essence a kind of table. Therefore, here I will also show you how to create table with RTF code.

You can see that RTF header in this example is complicated when comparing against that of above example. It introduces new elements such as font table and color table. They determine the font or color of unformatted text.

In the document area, it specifies three rows and each row consists of different number of cells. There are two cells in the first row, three cells in the second row and fours cells in the last row.

Here is what you will see if you open the file in Microsoft Word. There is one table which consists of three rows and all rows are made of different number of cell. If you look at the code closely, you will find that we need to specify size of all cells before specify the content of cells no matter how many cells in a row. And the border of cells can be customized too. \brdrt indicates top border line, \brdrs means that the style of border line is single line, \brdrw40 determines the width of border line.  For the meaning of other control words, please read RTF specification.

Above two examples not only show you how to create RTF files, but also show you potential to manipulate RTF code to customize rtf file.

Read RTF file into SAS

We all know that SAS can be used to produce RTF file. It will be wonderful if SAS can also be used to manipulate RTF code in order to customize RTF file. Luckily, SAS provides us an approach to read RTF file into SAS. Suppose that the rtf file in our second example is saved as sample.rtf. Below figure shows you the full code to covert RTF file into SAS.

After submitting above code, you will get a SAS dataset like below. It is almost the same as that in txt file, right?

Convert SAS dataset into RTF file

It is a SAS dataset now and we can use SAS statement to manipulate RTF code and thus modify RTF file. Following code removes data (the first row of table in essence) from 23rd to 27th row. And after putting rtf code into original RTF file, we can see that the first row in the table has been removed. This is in consistence with what did with RTF code via SAS.

Note:

RTF code will be a mess if you save RTF file in Microsoft Word and thus cannot be read into SAS for manipulation.