Skip to content

Instantly share code, notes, and snippets.

@rudvfaden
Created May 25, 2021 06:56
Show Gist options
  • Save rudvfaden/5dba82df3d59464661c7232c2e5d1996 to your computer and use it in GitHub Desktop.
Save rudvfaden/5dba82df3d59464661c7232c2e5d1996 to your computer and use it in GitHub Desktop.
Tries to count nobs by metadata, else default to recusion count.
%macro nobs(data=_last_);
%local dsid anobs whstmt counted rc;
%let DSID = %sysfunc(open(&DATA., IS));
%if &DSID = 0 %then %do;
%put %sysfunc(sysmsg());
%let counted = .;
%goto mexit;
%end;
%else %do;
%let anobs = %sysfunc(attrn(&DSID, ANOBS));
%end;
%if &anobs=1 %then
%let counted = %sysfunc(attrn(&DSID, NLOBS));
%else %do;
%put INFO: Observations in "&DATA." must be counted by iteration.;
%let counted = 0;
%do %while (%sysfunc(fetch(&DSID)) = 0);
%let counted = %eval(&counted. + 1);
%end;
%end;
%let rc = %sysfunc(close(&DSID));
%MEXIT:
&COUNTED.
%mend nobs;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment