Reputation: 706
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?
Upvotes: 2
Views: 1692
Reputation: 8513
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.
Upvotes: 0
Reputation: 63424
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.
Upvotes: 1