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_1 | 40 |
Data_2 | 30 |
Data_3 | 230 |
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
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!
Many thanks for your prompt answer.
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!
Aamzing.
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
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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.