How to automatically count #rows of multiple data set and store each data set count in table

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

How to automatically count #rows of multiple data set and store each data set count in table

Hi,

 

I am new to SAS programming. I just try to find optimization code for one my exercise.

 

I have 3 SAS data set in my library.  eg: data_1,data_2 and data_3.

 

what i want do that i need count each data set rows and store in table.

 

expected result:

Table#Rows
Data_140
Data_230
Data_3230

 

i can simply do like this:

 

Proc sql;

create table ds.report  as

select 'Data_1' as Table, count(*) from ds.Data_1

union

select 'Data_1' as Table, count(*) from ds.Data_2

union

select 'Data_1' as Table, count(*) from ds.Data_3;

quit;

 

this solution basic school level and okay for small count of file. but i looking for simple code.

 

eg: I can easily do this in qlikview.

 

Report:

Load

Select tablename() as Table, count(*) from Data_*;

 

Looking forward your advise for this case.

 

Best,

Robert

 

 

 


Accepted Solutions
Solution
3 weeks ago
Super User
Super User
Posts: 8,174

Re: How to automatically count #rows of multiple data set and store each data set count in table

Posted in reply to robert_bos

Per @RM6 great answer, the answer to your question is simply that you do not need to.  SAS has its own internal metadata tables, which you can find in SASHELP library.  VTABLE contains information on all the tables in all the libraries that have been set, and VCOLUMN for all the columns within them, so you can simply query those tables, or in fact the dictionary syntax in SQL also relates to these:

http://www2.sas.com/proceedings/sugi30/070-30.pdf

So datastep would be:

data want;
  set sashelp.vtable (where=(libname="YOURLIB" and substr(memname,1,5)="DATA"));
run;

I would also really advise against using reserved words (data in this case) as a dataset name.  It both looks odd, and doesn't provide any information, we know its data, its a dataset, what does it contain! 

View solution in original post


All Replies
Contributor RM6
Contributor
Posts: 24

Re: How to automatically count #rows of multiple data set and store each data set count in table

Posted in reply to robert_bos
Try this

proc sql;
create table xyz as
select memname,nobs from dictionary.tables where upcase(libname)='WORK' and upcase(Memname) in ('DATA_1','DATA_2','DATA_3');
quit;



New Contributor
Posts: 4

Re: How to automatically count #rows of multiple data set and store each data set count in table

Many thanks for your prompt answer.

Solution
3 weeks ago
Super User
Super User
Posts: 8,174

Re: How to automatically count #rows of multiple data set and store each data set count in table

Posted in reply to robert_bos

Per @RM6 great answer, the answer to your question is simply that you do not need to.  SAS has its own internal metadata tables, which you can find in SASHELP library.  VTABLE contains information on all the tables in all the libraries that have been set, and VCOLUMN for all the columns within them, so you can simply query those tables, or in fact the dictionary syntax in SQL also relates to these:

http://www2.sas.com/proceedings/sugi30/070-30.pdf

So datastep would be:

data want;
  set sashelp.vtable (where=(libname="YOURLIB" and substr(memname,1,5)="DATA"));
run;

I would also really advise against using reserved words (data in this case) as a dataset name.  It both looks odd, and doesn't provide any information, we know its data, its a dataset, what does it contain! 

New Contributor
Posts: 4

Re: How to automatically count #rows of multiple data set and store each data set count in table

Aamzing.

New Contributor
Posts: 4

Re: How to automatically count #rows of multiple data set and store each data set count in table

Posted in reply to robert_bos

I would also really advise against using reserved words (data in this case) as a dataset name.  It both looks odd, and doesn't provide any information, we know its data, its a dataset, what does it contain! 

 

hahaha.  I just give it for an example but real name is different. thanks for your advise.

 

Best,

Robert

Occasional Contributor
Posts: 10

Re: How to automatically count #rows of multiple data set and store each data set count in table

Posted in reply to robert_bos

Just be aware that using dictionary tables would not work for tables where the SAS engine does not have access to the number of rows in the table. This would be the case for database tables, and also SAS views of SAS tables. You would need to use count(*), or equivalent, in these cases.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 289 views
  • 7 likes
  • 4 in conversation