Sending automated email from SAS with attachments that changes name

791 views Asked by At

I have files that should be send out every week. These files changes names e.g. "filename_1" next week it will be "filename_2".

But it only takes the filename that I manually wrote which is filename_1. Is there a way to say that it should take the latest file with this name everyweek,instead of me doing it manually every week?

This is my code for the email (I manually wrote the filename):

filename outbox email from="[email protected]" to="[email protected]" type='text/html' subject='test' attach=("F:\filename_1.png" ct='png') ods html body=outbox rs=none style=Htmlblue;run; ods html close;

1

There are 1 answers

0
Stu Sztukowski On

The SAS macro facility will help you solve this very problem. If your filenames always have a consistent pattern, you can assign a macro variable to automatically change it for you. For simplicity's sake, let's say your filename always ends with today's date. You can assign a macro variable to hold this value.

%let filename = filename_&sysdate9..png;

This will resolve to filename_14DEC2020.png. You can confirm it with %put &filename.

If your file is sent out weekly and increments in a pattern, some quick math will help us figure out the correct suffix. Let's set a base week to start. We can count the number of weeks from this base week to identify the suffix. In this case, let's say it's today: December 14th, 2020. intck() can count the number of weeks from then until today. Our logic is:

suffix = (Number of weeks from Dec. 14th 2020 to Today) + 1.

In data step language, this is:

suffix = intck('week', '14DEC2020'd, today() ) + 1;

Translated to SAS macro language:

%let suffix   = %sysevalf(%sysfunc(intck(week, %sysfunc(inputn(14DEC2020, date9.)), %sysfunc(today()) )) + 1);
%let filename = filename_&suffix..png;

Because we're pulling from data step functions, we need to enclose nearly everything in %sysfunc() to call them. This is one of the functions available that connect the SAS macro facility with the data step language.

Note that we also cannot use date literals directly in the SAS macro facility. We must use inputn() or putn() to convert a human-readable date into a SAS date format.

Simply call this macro variable within your code and it will resolve automatically (except within single quotes).

filename outbox email 
    from="[email protected]" to="[email protected]" 
    type='text/html' 
    subject='test' 
    attach=("F:\&filename" ct='png') 
;