Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tips and Tricks for Using Macros to Automate SAS Reporting.

Similar presentations


Presentation on theme: "Tips and Tricks for Using Macros to Automate SAS Reporting."— Presentation transcript:

1 Tips and Tricks for Using Macros to Automate SAS Reporting.
Nathan Risk (With help from MWSUG)

2 The Basic Problem I needed a daily report that would –
Produce a table of each account type and the daily change in balance. Put appropriate dates on that table. Compute the daily change in balance for each checking account. Make a list of each account with a large change of balance. the entire list to a group of people and include a running graph of deposit totals. any accounts that are in Private Banking to the appropriate private banker. I would like to be able to date those reports. I wanted to automate this - There is no need for me to input information. There is no need for me to modify the code before running. There is no need to import any outside files. I had a lot of this done before MWSUG, but there were a few great presentations and a session of “code doctors” that helped me out.

3 What I Want to Show You. How to easily put dates into Macros.
How to create and read useful macro variables from your data set. How to get SAS to make html s and pdf attachments.

4 What Day is It? today() But I want a macro! %let today = today();
No, I want a global macro! %let today = %sysfunc(today()); Now, format it for me! %let today=%sysfunc(today(),date9.); Thank you!

5 Let’s Email This! ods _all_ close; ods listing;
This closes the all output delivery systems and lets SAS know we are going to do something with the output. FILENAME mail SUBJECT="Daily Deposit Report for &today" CONTENT_TYPE="text/html"; This create the FILENAME mail and tells who to deliver it to. ODS HTML BODY=mail; This says that the body of mail will have SAS output.

6 Let’s Email This! data _null_; file print;
put "Any questions can be referred to Nathan Risk"; run; This just prints a line to the SAS output. I then have SAS output the tables I want to mail. ODS HTML CLOSE; Then we close the HTML;

7 Let’s Email (a Part of) This!
We want to individual Private Bankers their clients with a big movement in Money. PROC SQL; CREATE TABLE OFFICERS as SELECT t1.INCNTV_NB as DPDDM_MOFFICER, t1.FRST_NM, t1.LAST_NM, t1. _ADDR_TXT FROM REF.OFFICERS t1; QUIT;

8 Let’s Email (a Part of) This!
I do not trust account assignment entirely. So I want to make sure of who I am mailing to.

9 Let’s Email (a Part of) This!
I don’t want to have to import this file every time. LIBNAME PB XLSX "/sasemdata/bia/Projects/PB/PB_STAFF.xlsx"; data incentives; set PB.incentives; run; Warning! You cannot use the data viewer with SAS EG with these kinds of Tables.

10 Let’s Email (a Part of) This!
Then we can pull out anyone not on our list. I sorted the incentive data base and make a string DPDDM_OFFICER variable. data officers; merge Incentive_sorted (in=A) OFFICERS; by DPDDM_MOFFICER; if A; run; Now I am ready to individual private bankers. The table Days3 has all of the accounts with a big movement in them. data DAYS3; MERGE DAYS3 (in=A) OFFICERS (in=B); if B then GOOD_OFF=1; else GOOD_OFF=0; if GOOD_OFF=0 then do; DPDDM_MOFFICER=‘12347'; Officer=“Donald Duck"; end;

11 Let’s Email (a Part of) This!
proc sql; select distinct DPDDM_MOFFICER into :Officers separated by "|" from DAYS3; quit; select count(distinct(DPDDM_MOFFICER)) into :OFF_COUNT from DAYS3; &Officers is a macro variable that is a list of incentive numbers separated by ‘|’. &OFF_COUNT is a macro variable that tells us how may incentive numbers there are.

12 Let’s Get Loopy! %macro loop; %do i =1 % to &off_count; title;
proc SQL; select distinct trim( _ADDR_TXT) into : _TXT from DAYS3 where DPDDM_MOFFICER = "%scan(&OFFICERS,&i,|)"; quit; select distinct OFFICER into :OFFICER from DAYS3 %scan() reads through our macro variable. It normally divides a macro variable up by spaces, but we are instructing it to use ‘|’ instead. Macro variables can be 65,534 characters long so we are fine.

13 This Looks Familiar FILENAME mail EMAIL TO="%cmpres(&Email_TXT)"
SUBJECT="Daily Private Banker Deposit Big Mover Report for &today" CONTENT_TYPE="text/html"; ODS HTML BODY=mail; data _null_; file print; put "Any questions can be referred to Nathan Risk"; run; Note %cmprss. We are not in a data step so compress won’t work. %cmprss is a macro function that will work on global macro variables and remove all blanks.

14 Add in Our Tables PROC SQL; create table BIG_WINNERS_PB_SMALL as
select ACCT_NBR,PRDCT_DESC_NM,DPDDM_IFSHORT,BALANCE2,BALANCE1,DIFF,BRNCH_NM,MKT_NM,PB from DAYS3 and DPDDM_MOFFICER = "%scan(&OFFICERS,&i,|)" order by diff desc; run;

15 Wrap it Up! ODS HTML CLOSE; %end; %mend; %loop;
You need to close the ODS You need to close the loop You need to close the Macro You need to call the Macro

16 What about Graphs? Graphs do not work well in the SAS html/ world. They do not (or at least I cannot get them to) resolve. Instead we are going to make a PDF. We start as before – %let today=%sysfunc(today(),date9.); FILENAME mailbox SUBJECT="Daily Deposit Report for &today";

17 Let’s Make a PDF FILENAME pdffile "%SYSFUNC(GETOPTION(WORK))/Daily_Deposit_Report_&today..pdf"; GETOPTION tells the current status of an option. GETOPTION(WORK) returns the current work directory. Since we are outside of a datastep, we use %SYSFUNC to get it. Note the extra dot ODS LISTING CLOSE; ODS PDF FILE=pdffile; We close the ODS listing and direct ODS to the PDF file pfffile. We the create our PDF with normal SAS gplot and proc report or whatever you like. ODS PDF CLOSE; ODS LISTING; We close the pdf file and direct ODS back to listing. The pdf file looks cool in the log: NOTE: ODS PDF printed 19 pages to /sasemwork/SAS_work9EC _lkdefgh1/SAS_workE _lkpv1sasaa01/Daily_Deposit_Report_02NOV2017.pdf.

18 Let’s Make a PDF DATA _NULL_;
attachment=FINFO(FOPEN('pdffile'),'FileName'); FILE MailBox; PUT "The Daily Deposit Report is Attached"; PUT '!EM_ATTACH!' attachment; RUN; FINFO Returns the value of a file information item for an external file and FOPEN opens a file. We use the data _null_ step to build the file that gets sent to the server with the pdf file as an attachment.

19 Let’s Make a PDF

20 If you get stuck, you have options.
Options mprint; Options symbolgen;


Download ppt "Tips and Tricks for Using Macros to Automate SAS Reporting."

Similar presentations


Ads by Google