I've tried both approaches and if there are 0 records in the dataset the email is still being sent.
LIBNAME DWView META Library=DWView repname=Foundation host=coues user="&SPUsername" pw="&SPPassword";
FILENAME mail EMAIL
SUBJECT="Results as of %sysfunc(date(),yymmdd10.)"
TO= "myemail"
FROM= "myemail"
ATTACH="D:\Temp\nick_tmp\bad_grad_date.xls";
proc sql NOPRINT;
Create Table Work.bad_grad_date
as
select a.PERSON_ID_NB
,p.last_nm as HIGH_SCHOOL_NM
,i.CEEB_Cd as HIGH_SCHOOL_CD
,a.end_yr as Graduation_Dt
from (
SELECT piya.person_id, PERSON_ID_NB, piya.end_yr, max(institution_id) as institution_Id
FROM DWView.Person_Institution_Year_Attend_C PIYA
INNER JOIN (
select PIYA.person_Id, max(end_yr) AS GRAD_YR
from DWView.Person_Institution_Year_Attend_C PIYA
INNER JOIN DWVIEW.Institution_C I
on piYa.institution_id = i.institution_id
where i.education_type_cd in ('11','97','98')
and PIYA.END_YR NOT BETWEEN 1900 AND 2025
GROUP BY PIYA.PERSON_ID
) B
ON PIYA.Person_Id = B.Person_Id
AND B.Grad_Yr = PIYA.End_Yr
group by piya.person_id, piya.end_yr, PERSON_ID_NB
) a
inner join dwview.person_c p /* create join to get institution name */
on a.institution_Id = p.person_id
inner join dwview.institution_c i /* create join to get CEEB code */
on a.institution_id = i.institution_id
ORDER BY A.person_id_nb
;
quit;
PROC EXPORT
Data = Work.bad_grad_date
OUTFILE= "D:\Temp\nick_tmp\bad_grad_date.xls"
DBMS=XLS REPLACE;
data _null_;
if _n_ eq 0 and eof then stop;
file mail;
put "This is a test.";
stop;
set Work.bad_grad_date(drop=_all_) end=eof;
run;