By clicking on associated hyperlink, word can direct you quickly to a specific point identified by bookmark in a word document. As a programmer, you may would like to create hyperlink and bookmark in your rtf file sometimes. This post will introduce how to create then using PROC REPORT.

Introduction of Hyperlink and Bookmark

A hyperlink is a “hot spot” that allows you to jump to another location which can be another file on your hard disk or company’s network or an internet address or a location in the same word file.It is in essence a field. And syntax in rtf code for a hyperlink field is as below:

{\field {\*\fldinst HYPERLINK \\l “bkidx” } {\fldrslt yyy } }

Text following \fldinst is field instructuon and in this case “HYPERLINK” tells that this filed is a hyperlink. “bkidx” will direct you to a destination (such as bookmark) identified by “bkidx”. Text following \fldrslt is the text calculated for this hyperlink and this is what will be displayed for hyperlink.

Bookmark is a destination. RTF code \*\bkmkstart indicates the start of the specified bookmark while \*\bkmkend indicates the end of the specified bookmark. The syntax is as below:

{\*\bkmkstart “bkidx” } xxxx {\*\bkmkend “bkidx” }

Text between (xxxx in above example) between start of bookmark and end of bookmark is identified by “bkidx”. By clicking on hyperlink having same “bkidx”, word will direct you to xxxx in this example.

Sample SAS code to create Hyperlink and Bookmark

Click here to hide/show code


data class;
length name $200.;
set sashelp.class;
n = _n_;
num=strip(put(n, best.));

name = ‘\cf2{\field {\*\fldinst HYPERLINK \\l “bkidx’||strip(num)||'”}{\fldrslt ‘||strip(name)||’}}’;
run;
proc sort data=class; by sex age name; run;

data class2;
length name $200.;
set sashelp.class;
n = _n_;
num=strip(put(n, best.));

name = “\cf6{\*\bkmkstart bkidx”||strip(num)||”}”||strip(name)||”{\*\bkmkend bkidx”||strip(num)||”} “;
run;

ods listing close;
ods tagsets.rtf file=”C:\test.rtf”;
proc report data=class nowd style={protectspecialchars=off outputwidth = 100%};
column sex age name;
define sex / display ‘Sex’ style={cellwidth=5cm just=left};
define age / display ‘Age’ style={cellwidth=5cm just=left};
define name/ display ‘Name’ style={cellwidth=5cm just=left};
run;

proc report data=class2 nowd style={protectspecialchars=off outputwidth = 100%};
column name height weight;
define name /display ‘Table’ style={cellwidth=5cm just=left};
define height/display ‘Height’ style={cellwidth=5cm just=left};
define weight/display ‘Weight’ style={cellwidth=5cm just=left};
run;
ods targets.rtf close;
ods listing;


By running above code, you will get two page of rtf files as below, By clicking on hyperlink (Alice in our example) in first page, word will direct you bookmark destination (Alice in our example) in second page.

Here are from those two datasets: class and class2. You can find the relationship between hyperlink and bookmark by yourself.

Reminder:

Please note that protectspecialchars=off should be included in PROC REPORT to enable RTF code. Otherwise you will get an error.