07-08-2014 02:56 PM
I would like to check the record length of a dataset and if the record length > 0 , then email the data of the dataset in an email. Any idea how I can achieve this?
specifically, i need to know how to use if..then...else to attach a particular file
Thank you for your help.
%let file1 = "C:\sasdata\Output\delta\
%let file2 = "C:\sasdata\Output\delta\
%let total_rec1 = %nobs(WORK.DATASET1); /* This has been output to file1 */
%let total_rec2 = %nobs(WORK.DATASET2); /* This has been output to file2 */
/* Emailing the file */
filename mymail email
to = ('firstname.lastname@example.org' )
subject= "ATTACHMENT TEST"
attach= (&file1. &file2.)
put "Auto message generated from SAS - Do Not Reply...";
07-08-2014 03:27 PM
This worked for me. You can adapt it to fit your needs. The code looks for an empty dataset. If the file is empty, an email is sent to the code submitter, and the processing stops. Otherwise, the code continues processing.
filename mymail email;
subject=(" Table does not exist or has zero observations. &sysdate. ")
put " The Table OPS.SAS_NC does not exist or has zero observations. &sysdate. ";
%if %sysfunc(exist(&chekfil.)) %then %do;
/* PC SAS cannot read the metadata of the file in a remote server */
/* If the file exists, create a temporary dataset that will contain at least one? observation */
/* Then read the metadata NOBS number of the temporary dataset */
proc sql noprint;
drop table work.obscount;
create table work.obscount as
SELECT distinct a.recordid
FROM &chekfil. as a
SELECT distinct a.nobs into bscnt
FROM dictionary.tables as a
WHERE LIBNAME = "WORK" and memname = "OBSCOUNT"
proc sql noprint; drop table work.obscount; quit;
%if &obscnt. = 0 %then do;
07-08-2014 03:45 PM
Thanks for the code. It however does not solve my purpose since I want to be able to send one email with either 1 or 2 attachments depending on whether the datasets have any records.
Anyway I can do an if...then...else on the 'Attach' statement?
07-09-2014 02:29 PM
You can create macros that queries for file records and one query that looks to see how many files to attach.
does the first dataset exist and does it have records? create a macro variable that has true or false value.
does the second dataset exist and does it have records? create a macro variable that has true or false value.
%if macro1 = true and macro2 = true %then %do;
put mail code to send two attachments
%else %if macro1 = true and macro2 = false %then %do;
put mail code to send file1 only as attachment
%else %if macro1 = false and macro2 = true %then %do;
put mail code to send file2 only as attachment
put mail code to send an email with no attachments