FOCUS AREAS

SAS Output Delivery System (ODS) documentation

Base SAS

Generating Hyperlinks with SAS

Generating hyperlinks can be done a variety of ways using the SAS system. I will go over some of the easier ways to do this within SAS. Some are more dynamic than others; some link to other HTML files, some link to GIF files and others PDF files. Adding link-able output adds another dimension to your HTML files such as having the ability to further explore the data. Included are very simple example basically to just get the concept across. For general information on hyperlinks, consult an HTML tutorial. Below is a sample data set, a macro and a data step that will create separate HTML files based on a value in the data set. The files that we create from this are what we use in the examples to drill down to.

/* Create sample data sets SALES */

data sales;
    input region $  state $  sales;
cards;
East VA 1000
East DC 2000
East MD 3000
East NC 4000
West CA 5000
West CA 3000
West CA 4000
West AR 3000
South AL 4000
South GA 3000
South TN 2000
North NY 4000
;
run;

/* Create separate HTML files based on the value of the variable region */

%macro create(region);

ods html body="&region..html";
       proc print data=sales(where=(region="&region"));
       title "Report for region &region";
       run;
ods html close;

%mend;

proc sort data=sales out=newdsn nodupkey;
  by region;


/* The Call Execute statement executes the macro Create for each unique */
/* value of region, which passes the value of the variable region to    */
/* the macro variable Create as a parameter. This subsets the data set  */
/* and names the HTML file bases on the value of the variable region.   */


data _null_ ;
    set newdsn;
       by region;
         call execute('%create('||region||')');
run;

1)  The first example adds a link to a data set using a control data set of created with PROC FORMAT. A Control Data set is a format in data set structure that has at least the required variables START, LABEL, FMTNAME. The variable that we want to create a hyperlink for, we rename to the START variable. The tag created that builds the hyperlink will be renamed LABEL. Once this Control data set is created, we use the CNTLIN= option of PROC FORMAT to read this data set and create a format from it. The format name will be the value specified for the variable FMTNAME. Below is an example.

data one;
   set newdsn;
      retain fmtname '$test';
          rename region=start ;
             label='<A '||compress('HREF="'||region||'.html')
                  ||'">'||trim(region)||'</A>';
run;

/* The control data set one is read created the character format $test. */

proc format cntlin=one;
run;


/* The variable name is formatted with the format $test.  */

ods html body='temp.html';

proc means data=sales mean sum;
class region ;
format region $test.;
var sales;
run;

ods html close;

2)  Generating hyperlinks are very easy using PROC REPORT. Using REPORT, there are the URL, URLB, and the URLP attributes that can be used in the CALL DEFINE statement. The URL attribute creates a link based on a relative file. The URLB attribute appends the location based on the BASE= option on in the ODS HTML statement. The URLP attribute creates the link based on the path specified with the PATH= option on the ODS HTML statement. Below is an example of using this feature to dynamically generate hyperlinks.

ods html file="temp.html" ;


proc report data=sales nowd;

  column region  sales ;
  define region     /group width=80  ;
  define sales       /sum;


   /*------------------------------------------------------------ -       */
   /*-- This turns the value of REGION into  a HTML link .              --*/
   /*--  To have true pages to link to, execute the code in               */
   /*   beginning of the document.                                        */
   /*                                                                     */
   /*-------------------------------------------------------------------  */

  compute region ;
    href=trim(region)||".html";
    call define(_col_, "URLP", href);
    endcomp;


  run;

ods html close;


3)  In addition to the previously discussed methods, the PROC TEMPLATE procedure can be used to create hyperlinks as well. This is done using the TRANSLATE statement within the column definition. This works for procedures that have table templates or creating a table template and referencing it.

 proc template;
      define table test;
         define column a ;

            translate
                 _val_="East" into
                   '<a href="East.html">'||_val_||'</a>',
                 _val_="South" into
                   '<a href="South.HTML">'||_val_||'</a>',
                 _val_="West" into
                   '<a href="West.HTML">'||_val_||'</a>',
                 _val_="North" into
                   '<a href="North.HTML">'||_val_||'</a>';
          dataname=region;
       end;


      define column b;
         dataname=sales;
       end;

    end;
  run;

 proc means data=sales nway;
 class region ;
 var sales;
 output out=newdsn sum=;
 run;

 ods html body='temp.html';

   data _null_;
      length region $80.;
      set newdsn;
          file print ods=(template="test");
      put _ods_;
   run;

ods html close;




/* To make this more dynamic, this can be macro-tized by placing the values  of   */
/* region into macro variables. The values resolve inside the macro loop within   */
/* the TRANSLATE statement.                                                       */



/* Put the number of distinct regions in variable NUM */
/* from the data set work.sales.                      */


options mprint;
  proc sql noprint;
    select count(distinct region) into :num
     from work.sales;


/* create macro list1 to the ending range determined by */
/* the macro variable NUM.                              */


 select distinct(region) into : list1- : list%left(&num)
   from work.sales;

run;
quit;




%macro link;
  proc template;
       define table test;
          define column a;
            translate
              %do i=1 %to &num;
                 %if &i ne &num %then %do;
                   _val_="&&list&i" into
                       "<a href='&&list&i...html'>"||_val_||'</a>',
                 %end;
              %else %do;
                   _val_="&&list&i" into
                "<a href='&&list&i...html'>"||_val_||'</a>';
              %end;
          %end;
           dataname=region;
         end;


         define column b;
            dataname=sales;
         end;
    end;
    run;
%mend;
%link


proc means data=sales nway noprint;
class region;
var sales;
output out=newdsn sum=;
run;

ods html body='temp.html';

   data _null_;
      length region $80.;
      set newdsn;
          file print ods=(template="test");
      put _ods_;
   run;

ods html close;


3A)  Hyperlinks can also be created with the URL attribute within PROC TEMPLATE.

Creating hyperlinks with the title and footnote statements can be done by adding the this tags to the title or footnote statement to add this individually, or by using the URL= attribute to add the hyperlinks globally to the title and footnote statements.

The Link option can be used to specify a link, or directly adding the tag to the title statement.

      Title link='http://www.sas.com' 'This is a test';

      Title '<a href='http://www.sas.com'>This is a test</a>';

      

  To add the link globally, the URL option can be specified within the style element SystemTitle for titles and the style element systemFooter for footnotes.

proc template;
   define style styles.test;
       parent=styles.default;
           style SystemTitle from TitlesandFooters /
                url='http://www.sas.com';
   end;
run;

4)  Creating hyperlinks/Drill-downs with SAS/GRAPH can be done by creating the hyperlink as the value of a variable in the data step and reference the variable in the HTML= option on the VBAR/HBAR statements.

See an example in FAQ 3592.

5)  To link to graphics files from a detail report can be done by either linking to the HTML file if the graphics files were created with ODS, or by linking to the GIF or JPEG files.


 /* The below macro create a graph and HTML file based on the     */
 /* value of the variable. Each region will generate an HTML      */
 /* file. The graphs can then be called from the from the link    */

%macro create(region);

ods html body="&region..html";
      goptions device=gif;
       title "graph of the &region region";
       proc gchart data=sales(where=(region="&region"));
       hbar region / sumvar=sales;
   run;
  quit;
ods html close;

%mend;

proc sort data=sales out=newdsn nodupkey;
  by region;

/* The Call Execute statement executes the macro Create for each unique */
/* value of region, which passes the value of the variable region to    */
/* the macro variable Create as a parameter. This subsets the data set  */
/* and names the HTML file bases on the value of the variable region.   */




data _null_ ;
    set newdsn;
       by region;
         call execute('%create('||region||')');
run;

/* Data step that creates the hyperlinks in the PROC MEANS output */

data one;
 length region $80.;
  set sales;
   region='<A '||compress('HREF="'||region||'.html')
         ||'">'||trim(region)||'</A>';
run;


ods html body='temp.html';

proc means data=one;
class region ;
var sales ;
run;

ods html close;

6)  The example below shows how to create a hyperlink with ODS & SAS/INTRNET. This format can be used with graph and tables. This builds the below call.

http://localhost/scripts/broker.exe?_service=default&_program=sample.webdrill .sas&region=Canada

data shoes;
  length region $200;
  set webdata.shoes;
  region='< href="' || "&_URL" || '?_service=' || "&_SERVICE" ||
             '&_program=sample.webdrill.sas&region=' || '"';
             urlencode(trim(region)) || '">' ||
  reglink= '<a ' || trim(reghref) || '>' ||
           trim(region)|| '</a>';
run;