Is there a way to detect when you've reached the last observation in a SAS DATA step?

sas
chucknelson picture chucknelson · Sep 28, 2009 · Viewed 18.9k times · Source

Is there a way to check how many observations are in a SAS data set at runtime OR to detect when you've reached the last observation in a DATA step?

I can't seem to find anything on the web for this seemingly simple problem. Thanks!

Answer

Chang Chung picture Chang Chung · Sep 28, 2009

The nobs= option to a set statement can give you the number of observations. When the data step is compiled, the header portion of the input datasets are scanned, so you don't even have to execute the set statement in order to get the number of observations. For instance, the following reports 2 as expected:

/* a test data set with two observations and no vars */
data two;
  output;
  output;
run;

data _null_;
  if 0 then set two nobs=nobs;
  put nobs=;
run;
/* on log
nobs=2
*/

The end= option sets a flag when the last observation (for the set statement) is read in.

A SAS data set, however, can be a SAS data file or a SAS view. In the case of the latter, the number of observations may not be known either at compile time or at execution time.

data subclass/view=subclass;
  set sashelp.class;
  where sex = symget("sex");
run;

%let sex=F;
data girls;
  set subclass end=end nobs=nobs;
  put name= nobs= end=;
run;
/* on log
Name=Alice nobs=9.0071993E15 end=0
Name=Barbara nobs=9.0071993E15 end=0
Name=Carol nobs=9.0071993E15 end=0
Name=Jane nobs=9.0071993E15 end=0
Name=Janet nobs=9.0071993E15 end=0
Name=Joyce nobs=9.0071993E15 end=0
Name=Judy nobs=9.0071993E15 end=0
Name=Louise nobs=9.0071993E15 end=0
Name=Mary nobs=9.0071993E15 end=1
*/