sas® email data driven application

Upload: fffresh

Post on 02-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 SAS Email Data Driven Application

    1/9

    SAS Email Data Driven Appl icationWenjie Wang, SunSolutionsInc, Clinton, NJJ ade Huang, Merck & Co., Inc., Rahway, NJ

    Simon Lin, Eisai Inc., Woodcliff Lake, NJ

    ABSTRACT

    In the business world, emails are a common and relied upon method of communication. For people who workwith SAS, the SAS EMAIL access method offers a convenient way to distribute a query result via email. Thispaper presents a useful technique on how to dynamically create the query report for the corresponding recipientand automate sending of a message with the attached file to numerous addressees on a regular basis. Thetargeted audiences are intermediate or advanced users of SAS.

    SAS Version 8.2 or 9.1, Windows XPKey Words: SAS, DATA step, FILENAME statement, EMAIL access method.

    INTRODUCTIONSending out bulk emails to multiple recipients with corresponding attachments from email software such asMicrosoft Outlook is not an easy task. Bulk emails can be error-prone as it is not unheard of to inadvertentlyattach and send an incorrect file to the wrong recipient. With SASs data step and EMAIL facilities, the email taskcan be automated with the simple implementation of SAS code. This paper presents an easy data driven SAS

    email application using SAS EMAIL capabilities.

    SAS EMAIL ACCESS METHOD BASICS

    System Configuration:Following is a list of SAS Email related system options: EMAILSYS, EMAILHOST, EMAILPORT, EMAILID, andEMAILPW. These are the values that are supported by EMAIL Access Method for system option EMAILSYS:

    o MAPI: Messaging Application Program Interface (interface supported by Windows and WindowsNT, which is used by Microsoft Exchange. MAPI is the default.)

    o VIM: Vendor Independent Mail such as Lotus or cc:Mail.

    The statement below can be issued to determine the value for a system:procoptionsoption=emailsys; run;Log:

    EMAILSYS=MAPI Used by E-mail Access Method and Send menu item to set theinterface type with underlying e-mail system.

    If a system supports the Email Access Method, sending emails from within a SAS session can be achieved.

    Syntax: FILENAME fileref EMAIL 'address' ;fileref : is a valid fileref

    o EMAIL: is the device-type keyword that indicates the request to use emailo 'address' : is the valid destination email address of the user for the email recipiento email-options specify the recipient's email address, subject, cc recipient's email address, and

    attachment filesOptions specified in the FILE statement inside the DATA step can override any corresponding options that werespecified in the FILENAME statement.Common directives to specify the message attributes in the DATA step PUT statement are:

    o !EM_TO! addresseso !EM_CC! addresseso !EM_SUBJ ECT! Subjecto !EM_ATTACH! filename.ext

    The following are the common directives that perform actions:o !EM_SEND!: sends the message with the current attributeso !EM_ABORT!: aborts the current messageo !EM_NEWMSG!: clears all attributes of the current message that were set using PUT statement

    directives

    1

    Applications DevelopESUG 2010

  • 7/27/2019 SAS Email Data Driven Application

    2/9

    Example 1: The following example shows how to issue a simple message with an attachment file using theFILENAME options:filename outmail email"[email protected]"

    subject="Lab Outlier Report.doc"cc="[email protected]"attach="c:\reports\Lab Outlier Report.doc";

    data_null_;file outmail;

    put'Dear Investigator,';put'Attached is your site Lab Outlier Report for study 0938-45';put'Please check and let me know if you have any question';put;put'Regards,';put;put'J ade Huang'; run;

    Screen shot from Outlook:

    Example 2: Use Email options specified in FILE statement to override the options in a FILENAME statement.filename outmail email"[email protected]";data_null_;file outmail

    to="[email protected]"cc="[email protected]"subject="Invoice Report.doc"attach="c:\reports\Invoice report.doc" ;

    put'Dear Wenjie';put'Attached is your last month invoice report.';put'If you have any question, please feel free to call.';put;put'Regards,';put'J ade Huang';run;

    The following screen shot shows that the option in the FILENAME statement is overwritten.

    2

    Applications DevelopESUG 2010

  • 7/27/2019 SAS Email Data Driven Application

    3/9

    Example 3: The directives in the PUT statement will override the corresponding option defined at theFILENAME or FILE statement as shown below.filename outmail email ;data _null_;

    file outmail;PUT'!EM_TO! [email protected]';PUT'!EM_SUBJ ECT! Reminder';PUT'!EM_CC! [email protected]';

    PUT'!EM_ATTACH! c:\reports\Insurance Claim Report.doc';put'Dear Accountant,';put'This is a reminder, please process the attached invoice within';put'7 days to claim money from the insurance provider.';put;put'Regards,';put;put 'J ade Huang';

    run;

    SAS EMAIL DATA DRIVEN APPLICATIONSending files to various people on a regular basis through Outlook can be difficult and may result in such errorsas sending incorrect files to the wrong person. With the SAS data step and the directive commands, the user candynamically modify the email option based on each observation of the input dataset.

    Example: Use SAS dataset and directives in put statements to automate the task.recipient email_address attach_file cc_addressWenjie Wang wenjie.wang@

    klconsultingservices.comu:\2009\RegistrationReport.doc

    [email protected]

    filename myemail email;data_null_;

    set indata.ex4; *input sas dataset;

    file myemail;* use input dataset variables in directives to specify the attributes;

    put'!EM_TO!' email_address;PUT'!EM_CC!' cc_address;put'!EM_SUBJ ECT! Registration Report for ' recipient;put'!EM_ATTACH! ' attach_file;put"Dear " recipient ":";put'Attached please find your Registration Report.';put'Regards,';put'J ade Huang';

    run;

    3

    Applications DevelopESUG 2010

  • 7/27/2019 SAS Email Data Driven Application

    4/9

    The following i s an example of a requirement for a SAS Email App lication:In a hospital, there are SOPs regarding Residents Notes Writing which regulate that each attending residentphysician write a note within 24 hours of each admission; the notes could be written by his/her assistant or nurse.If he/she did not write notes within 24 hours of an admission, at the end of that week, he/she will get an email withan attachment which shows the admission case lacking notes. This process can be automated with the e-management tool to increase the SOP compliance rate; macros are used to accomplish this task.

    The macro will have two parameters called FROM and TO which denote the report period from which to extractdata; the detail coding is in the appendix.

    Input dataset- NOTE:Note_EIN could be either a doctor or nurse employee identifier

    Input dataset- ADMISSION: Here ADM_EIN is the attending Physician identification number

    Input dataset: EMP

    SAS Email App lication Process Flow Chart

    Noncompli (Non compliance admission cases):ein, admission_dt, patientid, patientname ,Ward,room_bed ,delay

    Emp (Employer information):Employee EIN, first, last name, email address, etc Execute macro

    %macro update_noncompli_emailFor each non compliant EIN

    Populate the dataset- Noncompli_email_list:Ein, Emp_name, email_address, attach_file

    Noncompli_ein(Physicians who are not incompliance):Ein, Firstname, Lastname,Email_address

    Send out bulk emails withattachments usingDataset- Noncompl i_email_list

    SAS Email App lication Procedure:1. Prepare Data: noncompli & noncompli_ein

    4

    Applications DevelopESUG 2010

  • 7/27/2019 SAS Email Data Driven Application

    5/9

    o Merge the admission and notes files and select the admission cases which lack notes duringa certain week range(Noncompli dataset) based on the two macro parameters: from date andto date in the format of DATE9.

    o Find out the physicians who are not in compliance: Noncompli_ein

    ein firstname lastname email_address1002 Wenjie Wang [email protected]

    2. %macro update_noncompli_email_list: generate an rtf file associated with each attending physician'snon-compliant admission cases and populate the noncompli_email_list table for sending out emaillater on. This part is the key part of the application.

    o Compile %macro update_noncompli_email_list( )o Prepare noncompli_email_list table structure:

    Ein emp_name email_address attach_fi le

    o Execute the macro %update_noncompli_email_list to populate the datasetnon_compli_email_list based on each iteration of the input dataset noncompli_ein:

    Data _NULL;

    set noncompli_ein; ein firstname lastname email_address

    /* use execute routine to pass each observation to a macro from data step */call execute( '%update_noncompli_email_list(ein='||ein||

    ',Physician_name='||compress(firstname)||'_'||compress(lastname)||', name='||compress(firstname)|| ' '||compress(lastname)||', email_address='||compress(email_address)|| ') );

    RUN;

    %MACRO update_noncompli_email_lis t( ein= , phys ician_name= , name= ,email_address=);

    filename attfile "c:\ Admission_Audit_Report_for_&physician_name._Wkof&to..rtf";

    ods listing close; ods rtf file=attfile bodytitle;%* create attachment for each selected non compliant physician, the output screen shotis as follows;

    Admission Audit Report for Wenjie Wang from 20JUL2009 to 27JUL2009

    The following is a list of admissions which has no notes as of 28JUL2009

    Admission Date/Time Patient ID Patient Name Ward

    Room

    and Bed Reason

    20JUL2009:08:01:00 789098099 James Patterson Internal B00301-A No Notes

    20JUL2009:09:01:00 235345691 Lisa Lowers Internal B00301-B Delayed Notes

    21JUL2009:11:20:00 987098000 Brian Jacquers Internal B00302-B No Notes

    ods rtf close; ods listing;%* insert into noncompli_email_list ;

    Ein emp_name email_address attach_file1002 Wenjie

    [email protected]

    c:\paper\Admission_Audit_Report_for_Wenjie_Wang_Wkof27J UL2009.rtf

    %MEND update_noncompli_email_list;

    3. Email with an attachment using noncompli_email_list dataset as input:filename myemail email;data _null_;

    5

    Applications DevelopESUG 2010

  • 7/27/2019 SAS Email Data Driven Application

    6/9

  • 7/27/2019 SAS Email Data Driven Application

    7/9

    pr oc sor t data=i ndata. admi ssi on( wher e=( i nput ( "&f r om",date9. )

  • 7/27/2019 SAS Email Data Driven Application

    8/9

    %* t o macr o vari abl e *; f i l ename at t f i l e"u: \ 2009\ paper \ Admi ssi on_Audi t _Report

    _f or _&physi ci an_name. _Wkof &t o. . r t f " ;ods l i st i ng cl ose;ods rt f f i l e= at t f i l e bodyt i t l e;

    t i t l e1 "Admi ssi on Audi t Report f or &name f r om &f r om t o &t o. " ;t i t l e2 "The f ol l owi ng i s a l i st of admi ssi on whi ch has no not es

    or del ayed notes as of &t oday" ;

    pr oc sql ;sel ect admi ssi on_dt l abel =' Admi ssi on Dat e/ Ti me' ,pat i ent i d l abel =' Pat i ent I D' ,pat i ent name l abel =' Pat i ent Name' ,ward l abel =' War d' ,r oom_bed l abel =' Room and Bed' ,del ay l abel =' Reason' f ormat=del ayf .

    f r om noncompl iwher e ADM_EI N=&ei n;

    qui t ;ods rt f cl ose;ods l i s t i ng;

    %*** Now popul ate t he nonotes_emai l _l i st t abl e ***;

    pr oc sql ;i nser t i nt o noncompl i _emai l _l i stval ues ( &ei n, "&name" ,

    "&emai l _address" ,"u: \ 2009\ paper \ Admi ssi on_Audi t _Report _f or_&physi ci an_name. _Wkof &t o.. r t f " ) ;

    qui t ;%mend update_noncompl i _emai l _l i st ;

    %********************************************************************; %* St ep 4: f i nd out how many physi ci an i s not i n compl i ance *; %********************************************************************;

    proc sort data=noncompl iout =noncompl i _adm_ei n( keep=adm_ei n) nodupkey;

    by adm_ei n;run;proc sql;

    create t abl e noncompl i _ei n assel ect a. * f rom i ndat a. emp as a, noncompl i _adm_ei n as bwhere a. ei n=b. adm_ei n;

    quit;%********************************************************************; %* St ep 5: execut e macro t o generat e t he r eport f or each *; %* non- compl i ant physi ci an and updat e t he *; %* non_compl i _emai l _l i st dataset wi t h t he name and l ocat i on *; %* f or each r epor t f i l e, emai l _addr ess, physi ci an name, et c. *; *;

    %********************************************************************; proc sql;

    dr op t abl e noncompl i _emai l _l i st ;create t abl e noncompl i _emai l _l i st

    ( ei n num, emp_name char ( 60) , emai l _addr ess char ( 60) ,at t ach_ f i l e char ( 100) ) ;

    quit;

    Data _nul l ;set noncompl i _ei n;/ * execut i ng macro: updat e_noncompl i _emai l _l i st f r om dat a st ep */

    8

    Applications DevelopESUG 2010

  • 7/27/2019 SAS Email Data Driven Application

    9/9

    9

    cal l execut e( ' %update_noncompl i _emai l _l i st ( ei n=' | | ei n| |' , Physi ci an_name=' | | compr ess( f i r st name) | |' _' | | compr ess( l astname) | |' , name=' | | compr ess( f i r st name) | | ' ' | | compr ess( l astname) | |' , emai l _addr ess=' | | compr ess( emai l _addr ess) | | ' ) ' ) ;

    run;%********************************************************************; %* St ep 6: Send emai l t o each non- compl i ant physi ci an wi t h t he *;

    %* corr espondi ng at t achment *; %********************************************************************; name myemai l emai l ;f i l e

    data_nul l _;set noncompl i _emai l _l i st ;f i l e myemai l ;put ' ! EM_TO! ' emai l _address;put ' ! EM_SUBJ ECT! Admi ssi on Audi t Repor t f or ' emp_ name;put "Dear Pr ovi der " emp_ name ": " ;put "Accor di ng t o t he medi cal r ecord, you are ei t her t he " ;put "i npat i ent at t endi ng physi ci an or t he at t endi ng i n " ;put "char ge of t he war d f or t he pat i ent ( s) l i st ed bel ow. " ;put "An at t endi ng admi ssi on note i s r equi r ed by pol i cy " ;put "wi t hi n one cal endar day f r om t he t i me of admi ssi on, " ;

    put "but coul d not be f ound f or t he pat i ent ( s) l i st ed. " ;put "Pl ease pl ace your at t endi ng note i n syst em ASAP. " ;put "Thank you f or your at t ent i on. " ;put ' ! EM_ATTACH! ' at t ach_f i l e;put ' ! EM_SEND! ' ;put ' ! EM_NEWMSG! ' ;put ' ! EM_ABORT! ' ;

    run;

    %mend not es_r epor t ;%notes_report( f r om=20J UL2009, t o=27J UL2009) ;

    Applications DevelopESUG 2010