%macro ds2htm(htmlfile=,
      htmlfref=,
      openmode=replace,
      runmode=B,
      encode=Y,
      proploc=,
      bgtype=,
      bg=,
      septype=,
      seploc=,
      saspower=,
      brtitle=,
      ctext=,
      center=,
      twidth=,
      twunits=,
      talign=,
      border=,
      bwidth=,
      cpad=,
      cspace=,
      tbbgcolr=,
      obgcolr=,
      ibgcolr=,
      vbgcolr=,
      sbgcolr=,
      clbgcolr=,
      otag=,
      oface=,
      ocolor=,
      osize=,
      ohalign=,
      ovalign=,
      itag=,
      iface=,
      icolor=,
      isize=,
      ihalign=,
      ivalign=,
      iwrap=,
      vtag=,
      vface=,
      vcolor=,
      vsize=,
      vhalign=,
      vvalign=,
      vwrap=,
      stag=,
      sface=,
      scolor=,
      ssize=,
      shalign=,
      svalign=,
      cltag=,
      clface=,
      clcolor=,
      clsize=,
      clhalign=,
      clvalign=,
      clwrap=,
      ctag=,
      cface=,
      ccolor=,
      csize=,
      cvalign=,
      chalign=,
      ttag=,
      tface=,
      tcolor=,
      tsize=,
      ftag=,
      fface=,
      fcolor=,
      fsize=,
      data=,
      pw=,
      where=,
      obsnum=,
      labels=,
      formats=,
      round=,
      id=,
      var=,
      sum=,
      caption=);

%*********************************************************************;
%*                       *;
%*  MACRO: DS2HTM --  EXPERIMENTAL  --           *;
%*                       *;
%*  USAGE: %ds2htm(arg1=value, arg2=value, ... argN=valueN);   *;
%*                       *;
%*  DESCRIPTION:                   *;
%*  Experimental Software              *;
%*  This macro converts a SAS data set to an HTML table.     *;
%*                       *;
%*  NOTES:                     *;
%*  None.                    *;
%*                       *;
%*  SUPPORT: sasvcd                  *;
%*                       *;
%*  VERSION: 1.0                   *;
%*                       *;
%*********************************************************************;

%local htmlfile htmlfref openmode proploc bgtype bg septype seploc
   ctext center twidth twunits talign border bwidth cpad cspace
   tbbgcolr obgcolr ibgcolr vbgcolr sbgcolr clbgcolr oface
   ocolor osize ohalign ovalign iface icolor isize ihalign
   ivalign iwrap vface vcolor vsize vhalign vvalign vwrap sface
   scolor ssize shalign svalign clface clcolor clsize clhalign
   clvalign clwrap cface ccolor csize cvalign chalign data
   where obsnum labels formats round id var sum caption
   cbgcolr cwrap owrap swrap eflag notes runmode
   tface tcolor tsize fface fcolor fsize
   otag itag vtag stag cltag ctag ttag ftag
   pw encode saspower brtitle;

%*global _proploc;

%let notes=%sysfunc(getoption(notes));

%let cbgcolr=; %* unsupported by HTML;
%let cwrap=;  %* unsupported;
%let owrap=;  %* unsupported;
%let swrap=;  %* unsupported;

options nonotes;

%if "&runmode" eq "" %then %do;
  %put;
  %put ERROR: You must specify either I, B or S for execution mode.;
  %put;
  %goto exit;
%end;

%if (%upcase(&runmode) ne B) and
  (%upcase(&runmode) ne S) and
  (%upcase(&runmode) ne I)
%then %do;
  %put;
  %put ERROR: &runmode is an unknown or unsupported execution mode.;
  %put %str(   Specify either I, B or S.);
  %put;
  %goto exit;
%end;

%*;
%*  Check the syntax of the 4-level name. Existence will be handled
%*  in the SCL code.
%*;

%if "&proploc" ne "" %then %do;

  %if %length(%scan(&proploc, 1, '.')) gt 8 %then %do;
  %put;
  %put ERROR: &proploc is an invalid 4-level name for the SLIST entry;
  %put %str(   that contains the properties.);
  %put;
  %goto exit;
  %end;

  %if (%length(%scan(&proploc, 2, '.')) gt 8) or
  (%length(%scan(&proploc, 2, '.')) eq 0)
  %then %do;
  %put;
  %put ERROR: &proploc is an invalid 4-level name for the SLIST entry;
  %put %str(   that contains the properties.);
  %put;
  %goto exit;
  %end;

  %if (%length(%scan(&proploc, 3, '.')) gt 8) or
  (%length(%scan(&proploc, 3, '.')) eq 0)
  %then %do;
  %put;
  %put ERROR: &proploc is an invalid 4-level name for the SLIST entry;
  %put %str(   that contains the properties.);
  %put;
  %goto exit;
  %end;

  %if (%upcase(%scan(&proploc, 4, '.')) ne SLIST)
  %then %do;
  %put;
  %put ERROR: &proploc is an invalid 4-level name for the SLIST entry;
  %put %str(   that contains the properties.);
  %put;
  %goto exit;
  %end;

%end;

%*let _proploc=&proploc;

%if ("&htmlfile" eq "") and
  ("&htmlfref" eq "") and
  (%upcase(&runmode) eq B or %upcase(&runmode) eq S)
%then %do; %* no output HTML file specified;
  %put;
  %put ERROR: You must specify a file for the HTML output.;
  %put;
  %goto exit;
%end;

%if ("&htmlfile" ne "") and
  ("&htmlfref" ne "")
%then %do;
  %put;
  %put ERROR: You must specify a file OR fileref for the HTML output.;
  %put;
  %goto exit;
%end;

%if ("&openmode" eq "") and
  (%upcase(&runmode) eq B or %upcase(&runmode) eq S)
%then %do; %* no open mode for HTML file specified;
  %put;
  %put ERROR: You must specify a mode for opening the HTML output file.;
  %put;
  %goto exit;
%end;

%if (%upcase(&openmode) ne REPLACE) and
  (%upcase(&openmode) ne APPEND)
%then %do; %* invalid open mode for HTML file;
  %put;
  %put ERROR: &openmode is an unknown or unsupported open mode.;
  %put %str(   Specify either REPLACE or APPEND.);
  %put;
  %goto exit;
%end;

 /*****
%if ("&tabname" eq "") and
  (%upcase(&runmode) eq B or %upcase(&runmode) eq S)
%then %do; %* no table name specified;
  %put;
  %put ERROR: You must specify the 1 or 2-level name of the SAS data;
  %put %str(   file to be formatted.);
  %put;
  %goto exit;
%end;
 *****/

%*;
%*  Check the syntax of the 1 or 2-level name. Existence will be handled
%*  in the SCL code.
%*;

%if %length(%scan(&data, 1, '.')) gt 8
%then %do; %* check for libref > 8 chars;
  %put;
  %put ERROR: &data is an invalid name for the SAS data set;
  %put %str(   to be formatted.);
  %put;
  %goto exit;
%end;

%if %length(%scan(&data, 2, '.')) gt 8
%then %do; %* check for entry name > 8 chars;
  %put;
  %put ERROR: &data is an invalid name for the SAS data set;
  %put %str(   to be formatted.);
  %put;
  %goto exit;
%end;

%if %length(%scan(&data, 3, '.')) ne 0
%then %do; %* check for 3-level or higher table name;
  %put;
  %put ERROR: &data is an invalid name for the SAS data set;
  %put %str(   to be formatted.);
  %put;
  %goto exit;
%end;

%if (%upcase(&obsnum) ne N) and
  (%upcase(&obsnum) ne Y) and
  (%upcase(&obsnum) ne YES) and
  (%upcase(&obsnum) ne NO) and
  (%upcase(&obsnum) ne  )
%then %do; %* Y or N not specified for obs number flag;
  %put;
  %put ERROR: &obsnum is an unknown or unsupported value for the OBS number;
  %put %str(   flag. Specify either Y or N.);
  %put;
  %goto exit;
%end;

%if &obsnum ne
%then %let obsnum=%upcase(%substr(&obsnum, 1, 1));

%if (%upcase(&labels) ne N) and
  (%upcase(&labels) ne Y) and
  (%upcase(&labels) ne YES) and
  (%upcase(&labels) ne NO) and
  (%upcase(&labels) ne  )
%then %do; %* Y or N not specified for labels flag;
  %put;
  %put ERROR: &labels is an unknown or unsupported value for the LABELS;
  %put %str(   flag. Specify either Y or N.);
  %put;
  %goto exit;
%end;

%if &labels ne
%then %let labels=%upcase(%substr(&labels, 1, 1));

%if (%upcase(&formats) ne N) and
  (%upcase(&formats) ne Y) and
  (%upcase(&formats) ne YES) and
  (%upcase(&formats) ne NO) and
  (%upcase(&formats) ne  )
%then %do; %* Y or N not specified for formats flag;
  %put;
  %put ERROR: &formats is an unknown or unsupported value for the FORMATS;
  %put %str(   flag. Specify either Y or N.);
  %put;
  %goto exit;
%end;

%if &formats ne
%then %let formats=%upcase(%substr(&formats, 1, 1));

%if &round lt 0 and "&round" ne "" and "&round" ne "."
%then %do; %* negative value entered;
  %put;
  %put ERROR: &round is an invalid specification for the ROUND argument.;
  %put %str(   Specify a value of 0 or greater.);
  %put;
  %goto exit;
%end;

%if &round gt 10
%then %do; %* non-numeric value entered;
  %put;
  %put ERROR: &round is an invalid specification for the ROUND argument.;
  %put %str(   Specify a value of 10 or smaller.);
  %put;
  %goto exit;
%end;

%if "%upcase(%scan(&where, 1, ' '))" eq "WHERE"
%then %do; %* where clause starts with the word WHERE;
  %put;
  %put ERROR: Do not start WHERE clause with the word WHERE.;
  %put;
  %goto exit;
%end;

 /*****************************************************************

%if ("&bgcolor" ne "") and
  ("&bg"  ne "")
%then %do;
  %put;
  %put ERROR: Both background and background color were specified. Only;
  %put %str(   one specification is allowed.);
  %put;
  %goto exit;
%end;

%let bgtype=;

  %if "&bgcolor" ne "" %then %let bgtype=COLOR;
%else %if "&bg"  ne "" %then %let bgtype=IMAGE;

 *****************************************************************/

%if ("&bgtype" ne "")     and
  (%upcase(&bgtype) ne NONE)  and
  (%upcase(&bgtype) ne COLOR) and
  (%upcase(&bgtype) ne IMAGE)
%then %do;
  %put;
  %put ERROR: &bgtype is an invalid value for background type.;
  %put %str(   Specify NONE, COLOR or IMAGE.);
  %put;
  %goto exit;
%end;

%if ("&bgtype" eq "") and
  ("&bg"   ne "")
%then %do;
  %put;
  %put ERROR: You must specify a background type.;
  %put;
  %goto exit;
%end;

%if (%upcase(&bgtype) eq IMAGE) and
  ("&bg" eq "")
%then %do;
  %put;
  %put ERROR: You must specify an image location for the background.;
  %put;
  %goto exit;
%end;

%if (%upcase(&bgtype) eq COLOR) and
  ("&bg" eq "")
%then %do;
  %put;
  %put ERROR: You must specify a color for the background.;
  %put;
  %goto exit;
%end;

%if ("&septype" ne "")     and
  (%upcase(&septype) ne NONE)  and
  (%upcase(&septype) ne RULE)  and
  (%upcase(&septype) ne IMAGE)
%then %do;
  %put;
  %put ERROR: &septype is an invalid value for page break separator.;
  %put %str(   Specify NONE, RULE or IMAGE.);
  %put;
  %goto exit;
%end;

%if (%upcase(&septype) eq IMAGE) and
  ("&seploc" eq "")
%then %do;
  %put;
  %put ERROR: You must specify an image location for the page break separator.;
  %put;
  %goto exit;
%end;

%if ("&septype" eq "") and
  ("&seploc"  ne "")
%then %let septype =IMAGE;

%if "&encode" eq "" %then %do;
  %put;
  %put ERROR: You must specify either Y, or N for the ENCODE argument.;
  %put;
  %goto exit;
%end;

%if (%upcase(&encode) ne Y) and
  (%upcase(&encode) ne N) and
  (%upcase(&encode) ne YES) and
  (%upcase(&encode) ne NO)
%then %do;
  %put;
  %put ERROR: &encode is an unknown or unsupported ENCODE value.;
  %put %str(   Specify either Y or N.);
  %put;
  %goto exit;
%end;

%if &encode ne
%then %let encode=%upcase(%substr(&encode, 1, 1));

%if (%upcase(&center) ne Y) and
  (%upcase(&center) ne N) and
  (%upcase(&center) ne YES) and
  (%upcase(&center) ne NO) and
  (%upcase(&center) ne  )
%then %do;
  %put;
  %put ERROR: &center is an unknown or unsupported CENTER value.;
  %put %str(   Specify either Y or N.);
  %put;
  %goto exit;
%end;

%if &center ne
%then %let center=%upcase(%substr(&center, 1, 1));

 /************************************************************

%let tbgtype=;

%if "&tbbgcolr" ne "" %then %let tbgtype=COLOR;

 ************************************************************/

%if (%upcase(&twunits) ne PERCENT) and
  (%upcase(&twunits) ne PIXELS)  and
  (%upcase(&twunits) ne  )
%then %do;
  %put;
  %put ERROR: &twunits is an unknown or unsupported table width unit.;
  %put %str(   Specify either PERCENT or PIXELS.);
  %put;
  %goto exit;
%end;

%if ("&twidth"  eq "") and
  ("&twunits" ne "")
%then %do;
  %put;
  %put ERROR: No value specified for table width.;
  %put;
  %goto exit;
%end;

%if ("&twidth"  ne "") and
  ("&twunits" eq "")
%then %let twunits=Percent;

%if (%upcase(&talign) ne LEFT) and
  (%upcase(&talign) ne CENTER) and
  (%upcase(&talign) ne RIGHT)  and
  (%upcase(&talign) ne  )
%then %do;
  %put;
  %put ERROR: &talign is an unknown or unsupported table alignment.;
  %put %str(   Specify either LEFT, CENTER or RIGHT.);
  %put;
  %goto exit;
%end;

%if (%upcase(&border) ne Y) and
  (%upcase(&border) ne N) and
  (%upcase(&border) ne YES) and
  (%upcase(&border) ne NO) and
  (%upcase(&border) ne  )
%then %do;
  %put;
  %put ERROR: &border is an unknown or unsupported BORDER value.;
  %put %str(   Specify either Y or N.);
  %put;
  %goto exit;
%end;

%if &border ne
%then %let border=%upcase(%substr(&border, 1, 1));

%if ("&bwidth" ne "") %then %let border=Y;

 /***************************************************************
%if ("&obgcolr" ne "")
  %then %let obgtype=COLOR;
  %else %let obgtype=;

%if ("&ibgcolr" ne "")
  %then %let ibgtype=COLOR;
  %else %let ibgtype=;

%if ("&vbgcolr" ne "")
  %then %let vbgtype=COLOR;
  %else %let vbgtype=;

%if ("&sbgcolr" ne "")
  %then %let sbgtype=COLOR;
  %else %let sbgtype=;

%if ("&clbgcolr" ne "")
  %then %let lbgtype=COLOR;
  %else %let lbgtype=;

%if ("&oface"  ne "") or
  ("&ocolor" ne "") or
  ("&osize"  ne "")
  %then %let ofonttyp=OTHER;
  %else %let ofonttyp=;

%if ("&iface"  ne "") or
  ("&icolor" ne "") or
  ("&isize"  ne "")
  %then %let ifonttyp=OTHER;
  %else %let ifonttyp=;

%if ("&vface"  ne "") or
  ("&vcolor" ne "") or
  ("&vsize"  ne "")
  %then %let vfonttyp=OTHER;
  %else %let vfonttyp=;

%if ("&sface"  ne "") or
  ("&scolor" ne "") or
  ("&ssize"  ne "")
  %then %let sfonttyp=OTHER;
  %else %let sfonttyp=;

%if ("&clface"  ne "") or
  ("&clcolor" ne "") or
  ("&clsize"  ne "")
  %then %let lfonttyp=OTHER;
  %else %let lfonttyp=;

%if ("&cface"  ne "") or
  ("&ccolor" ne "") or
  ("&csize"  ne "")
  %then %let cfonttyp=OTHER;
  %else %let cfonttyp=;
 ***************************************************************/

%if &iwrap ne
%then %let iwrap=%upcase(%substr(&iwrap, 1, 1));
%if &vwrap ne
%then %let vwrap=%upcase(%substr(&vwrap, 1, 1));
%if &clwrap ne
%then %let clwrap=%upcase(%substr(&clwrap, 1, 1));
%if &cwrap ne
%then %let cwrap=%upcase(%substr(&cwrap, 1, 1));
%if &owrap ne
%then %let owrap=%upcase(%substr(&owrap, 1, 1));
%if &swrap ne
%then %let swrap=%upcase(%substr(&swrap, 1, 1));

*;
*  Perform rudimentary check for the sizes and other values entered.
*;

%let eflag=0;

data _null_;

array names(6) $20 ('observation number'  'ID variable'  'variable'
        'SUM variable'    'column label' 'caption');

array mvar1(6) $8  ('osize' 'isize' 'vsize' 'ssize' 'clsize' 'csize');

array mvar2(6) $8  ('ohalign' 'ihalign' 'vhalign' 'shalign'
        'clhalign' 'chalign');
array mvar3(6) $8  ('ovalign' 'ivalign' 'vvalign' 'svalign'
        'clvalign' 'cvalign');
array mvar4(6) $8  ('owrap' 'iwrap' 'vwrap' 'swrap'
        'clwrap' 'cwrap');

length tempnum 8 tempchar $200;

tempnum=0;

 /*******************************************************************
do i = 1 to dim(names);
  if symget(mvar1(i)) ne  '' then do;
  tempnum  = symget(mvar1(i));
  _error_ = 0;
  if tempnum eq . then do;
  put;
  put 'ERROR: Invalid specification for ' names(i) 'font size.';
  put;
  call symput("eflag", '1');
  STOP;
   end;
  end;
end;
 *******************************************************************/

if symget('twidth') ne '' then do;
  tempnum = symget('twidth');
  _error_ = 0;
  if tempnum lt 0 then do;
  put;
  put 'ERROR: Invalid specification for table width.';
  put;
  call symput("eflag", '1');
  STOP;
 end;
end;

if symget('bwidth') ne '' and symget('border') eq 'Y' then do;
  tempnum = symget('bwidth');
  _error_ = 0;
  if tempnum lt 0 then do;
  put;
  put 'ERROR: Invalid specification for table border width.';
  put;
  call symput("eflag", '1');
  STOP;
 end;
end;

if symget('cpad') ne '' then do;
  tempnum = symget('cpad');
  _error_ = 0;
  if tempnum lt 0 then do;
  put;
  put 'ERROR: Invalid specification for cell padding.';
  put;
  call symput("eflag", '1');
  STOP;
 end;
end;

if symget('cspace') ne '' then do;
  tempnum = symget('cspace');
  _error_ = 0;
  if tempnum lt 0 then do;
  put;
  put 'ERROR: Invalid specification for cell spacing.';
  put;
  call symput("eflag", '1');
  STOP;
 end;
end;

do i = 1 to dim(names);
  tempchar  = upcase(symget(mvar2(i))); * horizontal alignment;
  if tempchar not in ('CENTER' 'LEFT' 'RIGHT' '') then do;
  put;
  put ' ERROR: Invalid specification for ' names(i) 'horizontal alignment.';
  put;
  call symput("eflag", '1');
  STOP;
  end;
  tempchar  = upcase(symget(mvar3(i))); * vertical alignment;
  if tempchar ne '' then do;
  if names(i) eq 'caption' and
  tempchar not in ('BOTTOM' 'TOP')
  then do;
  put;
  put ' ERROR: Invalid specification for caption vertical alignment.';
  put;
  call symput("eflag", '1');
  STOP;
  end;
  end;
  if tempchar not in ('BASELINE' 'BOTTOM' 'MIDDLE' 'TOP' '') then do;
  put;
  put ' ERROR: Invalid specification for ' names(i) 'vertical alignment.';
  put;
  call symput("eflag", '1');
  STOP;
  end;
  tempchar  = upcase(symget(mvar4(i))); * wrap;
  if tempchar ne '' then do;
  if names(i) eq 'caption' then do;
  put;
  put ' ERROR: NOWRAP is not supported for captions.';
  put;
  call symput("eflag", '1');
  STOP;
  end;
  end;
  if tempchar not in ('Y' 'N' '') then do;
  put;
  put ' ERROR: Invalid specification for ' names(i) 'wrap argument.';
  put;
  call symput("eflag", '1');
  STOP;
  end;
end;

run;

%if &eflag eq 1 %then %goto exit;

%if (%upcase(&runmode) eq B or %upcase(&runmode) eq S) %then %do;
  proc display c=sashelp.htmlgen.tabgenb.scl; run;
%end;
%else %do;
  %if "&sysver" lt "6.12" %then %do;
  %put;
  %put ERROR: Interactive mode not supported prior to release 6.12.;
  %put;
  %goto exit;
  %end;
  proc display c=sashelp.htmlgen.tabgeni.frame; run;
%end;

%exit:
%*let _proploc=;
options &notes;

%mend ds2htm;
