• Print  |
  • Feedback  |

FOCUS AREAS

508 GRAPHS in SAS, Version 8

GACCESSIBLE Macro

Macro Usage

Macro Reference

Macro Code

EXAMPLES

One Graph in Output

Multiple Graphs

508 INFO


ODS Support

SAS Accessibility Statement

SAS, Version 8: 508 Graphs

The GACCESSIBLE Macro: Macro Code

/***********************************************************/
/*          CODE FOR THE GACCESSIBLE MACRO                 */
/*                                                         */
/* This program requires Version 8.2 or later of SAS/GRAPH */
/***********************************************************/

/***********************************************************/
/* This program defines the GACCESSIBLE macro, which can   */
/* be used in SAS/GRAPH to provide 508 accessible graphs.  */
/* For instructions on how to use the macro, see the       */
/* Usage Documentation.                                    */

%macro putline(line);
  %* this macro is called by the gaccessible macro;
  %let rc=%sysfunc(fput(&fid,&line));
  %let rc=%sysfunc(fappend(&fid));
%mend putline;

%macro gaccessible(fileref=,dsn=,vars=,desc=,hidden=false);
  %local dsid rc varcount i varname varnum vartype varfmt varlen
         varlist fid line unique string row source notes;

  %* adjust SOURCE and NOTES options;
  %let source=%sysfunc(getoption(source));
  %if &source eq SOURCE %then %str(options nosource;);
  %let notes=%sysfunc(getoption(notes));
  %if &notes eq NOTES %then %str(options nonotes;);

  %* make sure fileref exists and can be opened;
  %let fid=0;
  %if %length(&fileref) %then %do;
     %if %sysfunc(fexist(&fileref)) %then %do;
         %let fid=%sysfunc(fopen(&fileref,a));
         %if &fid eq 0 %then %do;
            %put (GACCESSIBLE) ERROR: The fileref %upcase(&fileref) cannot be opened.;
            %put %str(                     )%sysfunc(sysmsg());
            %goto exit;
         %end;
     %end;
     %else %do;
         %put ERROR: The file identified by the fileref %upcase(&fileref) does not exist.;
         %goto exit;
     %end;
  %end;
  %else %do;
     %put (GACCESSIBLE) ERROR: No Fileref provided for FILEREF parameter.;
     %goto exit;
  %end;

  %* open the data set;
  %let dsid=0;
  %let varcount=0;
  %let varlist=;
  %if %length(&dsn) %then %do;
     %let dsid=%sysfunc(open(&dsn));
     %if &dsid %then %do;
        %* check the VARS argument to make sure token is a variable;
        %let varname=%scan(&vars,1,%str( ));
        %let varcount=0;
        %let varlist=;
        %let i=1;
        %do %while(%length(&varname));
            %let varnum=%sysfunc(varnum(&dsid,&varname));
            %if &varnum %then %do;
                %let varlist=&varlist %sysfunc(varname(&dsid,&varnum));
                %let varcount=%eval(&varcount+1);
            %end;
            %else %put (GACCESSIBLE) WARNING: %upcase(&varname) is not a variable in %upcase(&dsn);
            %let i=%eval(&i+1);
            %let varname=%scan(&vars,&i,%str( ));
        %end;
     %end;
     %else %do;
         %put (GACCESSIBLE) ERROR: Dataset %upcase(&dsn) does not exist.;
         %goto exit;
     %end;
  %end;

  %if &varcount eq 0 and %length(&desc) eq 0 %then %do;
     %put (GACCESSIBLE) ERROR: No variables provided and no description provided.;
     %goto exit;
  %end;

  %* write Javascript independent of DSN and VARS;
  %let unique=%sysfunc(uuidgen(0,0));
  %let unique=%substr(&unique,1,8);

  %if %substr(%upcase(&hidden),1,1) eq T %then
     %putline(<div style="position:absolute; -moz-opacity:0.0; filter:alpha(opacity=0)">);
  %else
     %putline(<div>);

  %putline(<font size="1">);
  %putline(<a href="javascript:graphdata_&unique%str(%(%))">Click for description</a>);
  %putline(<hr>);
  %putline(</font>);
  %putline(</div>);
  %putline(<script language="javascript" type="text/javascript">);
  %putline(<!-- );
  %putline(function graphdata_&unique%str(%(%)){);
  %putline(%str(var _ie_ = %(navigator.userAgent.indexOf%("MSIE"%) > 0 %);));
  %putline(var _url_ = location.href;);
  %putline(var _pound = location.href.lastIndexOf%str(%("#"%)););
  %putline(%str(if %(_pound > -1%) {));
  %putline(%str(   _url_ = location.href.substring%(0, _pound%);));
  %putline(});
  %putline(%str(var _cont_ = _url_ + "#&unique";));
  %putline(%str(if %(_ie_ == true%) {));
  %putline(%str(   location.replace%(_cont_%);));
  %putline(});
  %putline(%str(document.open%(%);));
  %if %length(&desc) %then %do;
     %putline(document.writeln%str(%()"%superq(desc)"%str(%)););
  %end;
  %if &varcount gt 0 %then %do;
     %putline(document.writeln%str(%()"<p>The following table contains the data from the graph:<p>"%str(%)););
     %let string=;
     %do i=1 %to &varcount;
        %let string=&string<th scope=%str(%'col%')>%scan(&varlist,&i,%str( ))</th>;
     %end;
     %putline(document.writeln%str(%()"<table border><tr>&string</tr>"%str(%)););
     %do row=1 %to %sysfunc(attrn(&dsid,nobs));
        %let rc=%sysfunc(fetchobs(&dsid,&row));
        %let string=;
        %do i=1 %to &varcount;
           %let varname=%scan(&varlist,&i,%str( ));
           %let varnum=%sysfunc(varnum(&dsid,&varname));
           %let vartype=%sysfunc(vartype(&dsid,&varnum));
           %let varfmt=%sysfunc(varfmt(&dsid,&varnum));
           %if &vartype eq C %then %do;
              %if &varfmt eq %then %do;
                 %let varlen=%sysfunc(varlen(&dsid,&varnum));
                 %let varfmt=$&varlen..;
              %end;
              %let value=%sysfunc(getvarc(&dsid,&varnum));
              %let value=%sysfunc(putc(&value,&varfmt));
           %end;
           %else %do;
              %if &varfmt eq %then %let varfmt=best12.;
              %let value=%sysfunc(getvarn(&dsid,&varnum));
              %let value=%sysfunc(putn(&value,&varfmt));
           %end;
           %let string=&string<td>&value</td>;
        %end;
        %putline(document.writeln%str(%()"<tr>&string</tr>"%str(%)););
      %end;
      %putline(document.writeln%str(%()"</table>"%str(%)););
  %end;
  %putline(%str(if %(_ie_ == false%) {));
  %putline(%nrstr(document.write%("<p><a href=%'javascript:location.href=&quot;"%)));
  %putline(%str(document.write%(_cont_%);));
  %putline(%nrstr(document.writeln%("&quot;%'>Return</a>;"%) ));
  %putline(});
  %putline(%str(else {));
  %putline(%str(document.writeln%(%"<p><a href=%'javascript:window.history.back%(%)%'>Return</a>%"%) ));
  %putline(});
  %putline(%str(document.close%(%);));
  %putline(});
  %putline(//-->);
  %putline(</script>);
  %putline(%str(<a name="&unique"></a>));

  %exit:
  %* close stuff;
  %if &fid gt 0 %then %do;
     %let rc=%sysfunc(fclose(&fid));
  %end;
  %if &dsid gt 0 %then %do;
     %let rc=%sysfunc(close(&dsid));
  %end;

  %* restore NOTES and SOURCE options;
  options &source &notes;
%mend gaccessible;