BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8
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?
5 REPLIES 5
SPR
Quartz | Level 8 SPR
Quartz | Level 8
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 :obs 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
SASPhile
Quartz | Level 8
SPR,
will this display the datasetname and count ?
SPR
Quartz | Level 8 SPR
Quartz | Level 8
This displays name and count:
[pre]
%macro a;
%local i obs;
%do i=1 %to &N;
proc SQL noprint;
select COUNT(*) as obs into :obs from &&n&i
;quit;
%put DATASET=&&n&i obs=%TRIM(&obs);
%end;
%mend;
options nonotes;
%a
oprions notes;
[/pre]
SPR
SASJedi
Ammonite | Level 13
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;
Check out my Jedi SAS Tricks for SAS Users
Ksharp
Super User
[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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1701 views
  • 0 likes
  • 4 in conversation