Editor's note: This topic is very popular. data_null__ , @Bill, @sbb, @Ksharp and others have contributed great ideas. We've consolidated those here with a little more detail to help future readers.
For this kind of problem the question is not
- how many obs?
but
- are there zero obs?
You don't have to count anything -- just check for imediate EOF.
/* Init check flag */
%let dsempty=0;
/* destination for XLSX file, if generated */
filename outfile "%sysfunc(getoption(work))/class.xlsx";
/* create empty test data set by removing all records */
data class;
set sashelp.class;
run;
data class;
modify class;
remove;
run;
/* Check for empty data */
data _null_;
if eof then
do;
call symput('dsempty',1);
put 'NOTE: EOF - no records in data!';
end;
stop;
set class end=eof;
run;
%macro Export;
%if &dsempty. %then
%do;
%put WARNING: Export step skipped - no output records.;
%end;
%else
%do;
/*if not empty then execute the export proc*/
proc export data=class dbms=xlsx
file=outfile replace;
run;
%end;
%mend Export;
%Export;
Editor's note: This topic is very popular. data_null__ , @Bill, @sbb, @Ksharp and others have contributed great ideas. We've consolidated those here with a little more detail to help future readers.
For this kind of problem the question is not
- how many obs?
but
- are there zero obs?
You don't have to count anything -- just check for imediate EOF.
/* Init check flag */
%let dsempty=0;
/* destination for XLSX file, if generated */
filename outfile "%sysfunc(getoption(work))/class.xlsx";
/* create empty test data set by removing all records */
data class;
set sashelp.class;
run;
data class;
modify class;
remove;
run;
/* Check for empty data */
data _null_;
if eof then
do;
call symput('dsempty',1);
put 'NOTE: EOF - no records in data!';
end;
stop;
set class end=eof;
run;
%macro Export;
%if &dsempty. %then
%do;
%put WARNING: Export step skipped - no output records.;
%end;
%else
%do;
/*if not empty then execute the export proc*/
proc export data=class dbms=xlsx
file=outfile replace;
run;
%end;
%mend Export;
%Export;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.