DATA Step, Macro, Functions and more

number of observations

Reply
Super Contributor
Posts: 673

number of observations

In a library if there are 10 datasets and I would like to get the total number of observations from all the 10. is there any way other than using the proc sql and giving the each dataset name?
Super Contributor
Super Contributor
Posts: 365

Re: number of observations

Hello SASPhile,

This is my solution:
[pre]
proc SQL noprint;
select COUNT(distinct memname) as N into :N
from SASHELP.VMEMBER where libname="WORK" and memtype="DATA";
%let N=%TRIM(&N);
select distinct memname as name into :N1-:N&N
from SASHELP.VMEMBER where libname="WORK" and memtype="DATA";
quit;
%macro a;
%local i obs;
%global nobs;
%do i=1 %to &N;
proc SQL noprint;
select COUNT(*) as obs into Smiley Surprisedbs from &&n&i
;quit;
%if &i = 1 %then %let nobs=&obs;
%let nobs=%EVAL(&nobs+&obs);
%end;
%mend;
%a;
%put nobs=&nobs;
[/pre]
Sincerely,
SPR
Super Contributor
Posts: 673

Re: number of observations

SPR,
will this display the datasetname and count ?
Super Contributor
Super Contributor
Posts: 365

Re: number of observations

This displays name and count:
[pre]
%macro a;
%local i obs;
%do i=1 %to &N;
proc SQL noprint;
select COUNT(*) as obs into Smiley Surprisedbs from &&n&i
;quit;
%put DATASET=&&n&i obs=%TRIM(&obs);
%end;
%mend;
options nonotes;
%a
oprions notes;
[/pre]
SPR
SAS Employee
Posts: 104

Re: number of observations

Try something like this (replace the SASHELP libref with your libref of interest - make sure you type the libref in all caps):

proc sql;
title 'Total Observations for all datasets in the SASHELP library';
select sum(nlobs) 'Total Observations' format=comma16. as TotObs
from dictionary.tables
where libname ='SASHELP'
and MEMTYPE='DATA'
;
title 'Observations for each dataset in the SASHELP library';
select MEMNAME 'Dataset', sum(nlobs) 'Obs' format=comma16. as Obs
from dictionary.tables
where libname ='SASHELP'
and MEMTYPE='DATA'
group by MEMNAME
;
quit;
Super User
Posts: 10,041

Re: number of observations

[pre]
proc contents data=work._all_ out=sum(keep=memname nobs);
run;
data want(drop=total);
set sum end=last;
total+nobs;
output;
if last then do;
memname='Total';
nobs=total;
output;
end;
run;
[/pre]
Ksharp
Ask a Question
Discussion stats
  • 5 replies
  • 169 views
  • 0 likes
  • 4 in conversation