2

I'm under the impression that ins SAS 9.3 it's possible to embed SVG plots in HTML output files => SAS page on SVG output.

However, trying the following simple example on a Linux server always produces a HTML file with an external PNG file.

ods listing close;
options device=svg;
ods graphics / outputfmt=svg reset=all;
ods html path="~" file="01_output.html";

  proc sgplot data=sashelp.class;
    scatter x=height y=weight;
    ellipse x=height y=weight;
  run;

ods html close;
ods listing;

Does anyone know how to embed plots in HTML in SAS?

rambles
  • 706
  • 5
  • 9

2 Answers2

1

You need to either move reset=all to the start of your ODS GRAPHICS statement, or remove it. You're setting the format to SVG and then resetting it. The following worked on my machine (9.3 Windows), while your code produced a PNG.

ods listing close;
options device=svg;
ods graphics / outputfmt=svg;
ods html  path="c:\temp\" file="01_output.html";

  proc sgplot data=sashelp.class;
    scatter x=height y=weight;
    ellipse x=height y=weight;
  run;

ods html close;
ods listing;

In order to embed completely in the HTML file in one step, you need to be on 9.4 and use ods html5, which automatically embeds the svg. The options device=svg isn't necessary there.

Joe
  • 62,789
  • 6
  • 49
  • 67
  • Thanks, that's certainly better. That said, I'm now getting external SVG files rather than PNG ones - and they're not embedded in the HTML file. – rambles Apr 10 '14 at 14:55
  • Based on what I read from that page, I think that's what you are going to get. They don't mean `embed` as in "put all of the SVG into the HTML file directly", they mean "use the `` html element". – Joe Apr 10 '14 at 14:57
  • Specifically, "Using SAS/GRAPH, run your SAS program using the ODS HTML DEV=SVG statement. SAS creates the SVG document and the HTML file, embedding the SVG document in the HTML file using the element." – Joe Apr 10 '14 at 14:58
  • Yes, I think you're right. Researching this further suggests you can only embed SVG in HTML 5 files - and HTML 5' only available in SAS 9.4. Hmmm .. my search for a container file for plots and tables continuous (excluding page based docs like PDF and RTF). – rambles Apr 10 '14 at 17:42
  • Have you looked at ODS PACKAGE? – Joe Apr 10 '14 at 19:49
0

EDIT : The below ONLY applies to SAS 9.3 and prior. See Joe's selected answer above for how to do this in v9.4 onwards.

Yes it is possible to embed SVG into a .html file, but no this can't be done in a single step using ODS. ODS is always going to produce the SVG (or image) into a separate file then the .html it produces, and you will need to frankenstein them together yourself.

This article about using SVGs is lengthy but good:

http://css-tricks.com/using-svg/

And this question is also pretty useful:

Do I use <img>, <object>, or <embed> for SVG files?

Here's a duct-tape example (thanks to Joe whose code I slightly modified):

ods listing close;
options device=svg;
ods graphics / outputfmt=svg;

ods html  path="%sysfunc(pathname(work))" file="whatever.html";

  proc sgplot data=sashelp.class;
    scatter x=height y=weight;
    ellipse x=height y=weight;
  run;

ods html close;
ods listing;

The below creates a new HTML file called embedded.html which contains a very bare-bones .html file. It simply takes the contents of the SVG file and drops it in the middle of the file.

Because SVG is really just XML modern browsers should run this fine (but see the links above for how to get this working in older browsers).

data _null_;

  file   "%sysfunc(pathname(work))\embedded.html";
  infile "%sysfunc(pathname(work))\SGPlot1.svg" end=eof;

  if _n_ eq 1 then do;
    put "<html><body>";
  end;

  input;
  put _infile_;

  if eof then do;
    put "</body></html>";
  end;
run;

You mention in your comments also that you may want to do the same for other types of docs as well such as PDF/RTF. If so it may be worth posting a new question because you will have to encode things in base64 to achieve that and it's not a trivial exercise.

Community
  • 1
  • 1
Robert Penridge
  • 8,424
  • 2
  • 34
  • 55
  • Might want to annotate this: it can't be done *in SAS 9.3*, but in SAS 9.4 it can be embedded directly. – Joe Nov 21 '14 at 15:05