![]() |
Don't Be a SAS® Dinosaur:
Modernize Your SAS Programs
Zero Observations: Process an Empty Data Set |
You wish to perform some processing based on the number of observations in a data set.
Furthermore, you must handle cases in which there are no observations in the data set.
|
The original approach is to access the observation count through PROC SQL dictionary tables, loading a macro variable with the result and using the macro variable in conditional logic. Download this program
%macro checkNobs(dsn);
proc sql noprint;
select Nobs
into :ObsCount
from dictionary.tables
where trim(libname)||"."||memname="%upcase(&dsn)";
quit;
%if &ObsCount > 0 %then %do;
proc print data=&dsn(obs=10);
title1 "A few observations from &dsn";
run;
%end;
%else %do;
data _null_;
file print notitles;
put "There are no observations in &dsn";
run;
%end;
%mend checkNobs;
%checkNobs(SASHELP.CLASS) *Valid data set;
%checkNobs(SASHELP.DPLOG) *Empty data set;
SAS Log1119 %macro checkNobs(dsn); 1120 proc sql noprint; 1121 select Nobs 1122 into :ObsCount 1123 from dictionary.tables 1124 where trim(libname)||"."||memname="%upcase(&dsn)"; 1125 quit; 1126 %if &ObsCount > 0 %then %do; 1127 proc print data=&dsn(obs=10); 1128 title1 "A few observations from &dsn"; 1129 run; 1130 %end; 1131 %else %do; 1132 data _null_; 1133 file print notitles; 1134 put "There are no observations in &dsn"; 1135 run; 1136 %end; 1137 %mend checkNobs; 1138 %checkNobs(SASHELP.CLASS) *Valid data set; NOTE: There were 10 observations read from the data set SASHELP.CLASS. 1139 %checkNobs(SASHELP.DPLOG) *Empty data set; NOTE: 1 lines were written to file PRINT. SAS Listing OutputA few observations from SASHELP.CLASS Obs Name Sex Age Height Weight 1 Alfred M 14 69.0 112.5 2 Alice F 13 56.5 84.0 3 Barbara F 13 65.3 98.0 4 Carol F 14 62.8 102.5 5 Henry M 14 63.5 102.5 6 James M 12 57.3 83.0 7 Jane F 12 59.8 84.5 8 Janet F 15 62.5 112.5 9 Jeffrey M 13 62.5 84.0 10 John M 12 59.0 99.5 ======================================================================================== There are no observations in SASHELP.DPLOG |
![]() |
|
An alternate approach is to exploit the internal processing of the DATA step by obtaining the number of observations with the NOBS= option on the SET statement during the compile phase of the step and stopping the step immediately after using the retrieved value. Download this program
%macro checkNobs(dsn);
data _null_;
if NObs=0 then do;
call symput("ObsCount","0");
file print notitles;
put "There are no observations in &dsn";
end;
else call symput("ObsCount",put(NObs,best12.));
stop;
set &dsn nobs=NObs;
run;
%if &ObsCount > 0 %then %do;
proc print data=&dsn(obs=10);
title1 "A few observations from &dsn";
run;
%end;
%mend checkNobs;
%checkNobs(SASHELP.CLASS) *Valid data set;
%checkNobs(SASHELP.DPLOG) *Empty data set;
SAS Log
1151 %macro checkNobs(dsn);
1152 data _null_;
1153 if NObs=0 then do;
1154 call symput("ObsCount","0");
1155 file print notitles;
1156 put "There are no observations in &dsn";
1157 end;
1158 else call symput("ObsCount",put(NObs,best12.));
1159 stop;
1160 set &dsn nobs=NObs;
1161 run;
1162 %if &ObsCount > 0 %then %do;
1163 proc print data=&dsn(obs=10);
1164 title1 "A few observations from &dsn";
1165 run;
1166 %end;
1167 %mend checkNobs;
1168 %checkNobs(SASHELP.CLASS) *Valid data set;
NOTE: 0 lines were written to file PRINT.
NOTE: There were 10 observations read from the data set SASHELP.CLASS.
1169 %checkNobs(SASHELP.DPLOG) *Empty data set;
NOTE: 1 lines were written to file PRINT.
SAS Listing OutputSame as for the old way Advantages
of the
alternate
approach:
|
|
Back to SAS Dinosaur home page Printable copy of this page (without sample output)
SAS and all other SAS Institute Inc. product or service names are registered trademarks
or trademarks of SAS Institute Inc. in the USA and other countries.
|