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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.