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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.