Email SAS html output

2019-07-02 07:18发布

问题:

I am using SAS Enterprise Guide 6.1. I am trying to batch the program below with windows scheduler to produce a weekly report. It will have a few proc prints and sgplots. The user I am sending it to is high-level and does not have SAS (would not know what to do with the program if he did). Ideally, I'd like to just email him an attachment or even embed the plots/results in the email, so he can view them. The program I have seems to be creating the html file correctly, but when I open it in the email, the sgplots are not displaying. Any thoughts? Thanks.

filename temp email to="cthulhu@gmail.com"
                    subject="Testing the report"
                    type="text/html"
                    attach='/sasdata/cthulhu/email.html';

ods html path='/sasdata/cthulhu' file='email.html' gpath='/sasdata/cthulhu' style=htmlblue;

data have;
    input name $ class $ time score;
cards;
chewbacca wookie  1 97
chewbacca wookie 2 100
chewbacca wookie 3 95
saruman wizard 1 79
saruman wizard 2 85
saruman wizard 3 40
gandalf wizard 1 22
gandalf wizard 2 50
gandalf wizard 3 87
bieber canadian 1 50
bieber canadian 2 45
bieber canadian 3 10
;
run;

proc sql noprint;
    select count(distinct class) into :numclass
    from have;
    %let numclass=&numclass;
    select distinct class into :class1 - :class&numclass
    from have;
    select count(distinct name) into :numname
    from have;  
    %let numname=&numname;
    select distinct name into :name1 - :name&numname
    from have;
quit;

%macro printit;
%do i = 1 %to &numclass;
title "Report for &&class&i";
proc print data=have;
    where class="&&class&i";
run;
%end;
%mend;
%printit;

%macro plotit;
%do i = 1 %to &numname;
title "Plot for &&name&i";
proc sgplot data=have(where=(name="&&name&i"));
    series x=time y=score
    / legendlabel='score' markers lineattrs=(thickness=2);
    xaxis label="time";
    yaxis label='score';
run;
%end;
%mend;
%plotit;

ods html close;

data _null_;
   file temp;
   put 'This is a test';
run;

回答1:

The graphs are not showing because they are not available to the html viewer.

Options

  1. You could write the images to a place that is available and ensure the paths in the generated html are correct.

  2. Generate a PDF or RTF file and email that.

  3. Use SVG (scalable vector graphics). This will require you to parse the output files and embed the XML for the SVG into the HTML.



回答2:

There is one more alternative that DomPazz did not mention and I'll include it here for completeness. I do NOT recommend this approach unless you find yourself in the following situation:

  1. You are not allowed to use an attachment.
  2. The only locations you have available to place the images are either on an intranet or HTTPS server.
  3. The emails have to be readable from smartphones (which usually won't be connected to company VPN or able to supply HTTPS credentials to download and display the images).

Cons

  • This method is not supported directly by SAS.
  • It requires third party tools (such as BLAT - http://www.blat.net/).
  • HTML email messages are known to be pretty finicky when being rendered, so you must keep them simple.
  • It's not for the faint of heart.

Overview

The basic premise is that you create your charts as images on disk, create your HTML file on disk, and then use a 3rd party tool to send the email. The third party tool will embed the charts into the email, and the HTML you created will reference the embedded images using what is known as a CID (content-ID).

I suggest doing a little reading/googling on the terms 'html email cid' to become familiar with this first. You may also want to do a little reading up on base64 encoding and mime-types. You don't need to become an expert just learn enough to know what they are and roughly how they work.

Developing/Testing

You can still use ODS to create your .html file but be sure to use ODS HTML3 which forces SAS to use an older version of HTML. It also forces it to repeat the styling against each element. You need this because email clients such as Outlook actually use the MS Word engine to render HTML emails.

Once you have built your .html file and charts, open the .html file in a web browser and make sure it is displaying correctly.

Once you have your output displaying as desired in your web browser you still have a few more changes you need to make before you can send it as an email. When developing in your browser you would have noticed you embedded the image like this:

<img src="my_image.png" />

Well we can't do that for email, we need to use the CID link like this:

<img src="cid:my_image.png" />

When we use BLAT to send the email, we'll specify the images to embed. The image names will automatically be used for the CID: naming conventions.

You can send an email using BLAT like so:

blat test.html -f "myemailaddress@myemailaddress.com" -subject "test" -html -alttextf test.html -embed test.png -server mail.mysmtpserver.com -to myrecipient@recipentemail.com 

That's about it. Maybe if I get more time I'll provide a full piece of code with a working example.

Some useful reading:

http://24ways.org/2009/rock-solid-html-emails (READ THIS!!)

http://www.emailology.org

http://www.campaignmonitor.com/css/

HTML email align text



标签: html email sas