turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- number of observations

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

05-17-2011 01:14 PM

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?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to SASPhile

05-17-2011 01:41 PM

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 bs 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

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 bs 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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

05-17-2011 01:56 PM

SPR,

will this display the datasetname and count ?

will this display the datasetname and count ?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to SASPhile

05-17-2011 02:13 PM

This displays name and count:

[pre]

%macro a;

%local i obs;

%do i=1 %to &N;

proc SQL noprint;

select COUNT(*) as obs into bs from &&n&i

;quit;

%put DATASET=&&n&i obs=%TRIM(&obs);

%end;

%mend;

options nonotes;

%a

oprions notes;

[/pre]

SPR

[pre]

%macro a;

%local i obs;

%do i=1 %to &N;

proc SQL noprint;

select COUNT(*) as obs into bs from &&n&i

;quit;

%put DATASET=&&n&i obs=%TRIM(&obs);

%end;

%mend;

options nonotes;

%a

oprions notes;

[/pre]

SPR

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to SASPhile

05-17-2011 02:04 PM

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;

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to SASPhile

05-18-2011 06:16 AM

[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

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