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;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: