BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
robert_bos
Calcite | Level 5

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

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

6 REPLIES 6
RM6
Obsidian | Level 7 RM6
Obsidian | Level 7
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;



RW9
Diamond | Level 26 RW9
Diamond | Level 26

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! 

robert_bos
Calcite | Level 5

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

lethcons
Obsidian | Level 7

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.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 6 replies
  • 12439 views
  • 7 likes
  • 4 in conversation