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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—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.