DATA Step, Macro, Functions and more

How can I read the filenames from a library into a dataset?

Accepted Solution Solved
Reply
Contributor
Posts: 53
Accepted Solution

How can I read the filenames from a library into a dataset?

How can I read the filenames from a library into a dataset?

Thx,

Stefan


Accepted Solutions
Solution
‎08-13-2012 07:45 AM
Respected Advisor
Posts: 3,156

Re: How can I read the filenames from a library into a dataset?

Hi,

Since you mentioned 'library' instead of a folder/directory, I would assume you are trying to put data set names into a data set, and then there is a simple way: say you want every table names within 'work':

proc sql;

create table want as

select distinct memname as dname from dictionary.columns where libname='WORK';

QUIT;

Haikuo

View solution in original post


All Replies
Super Contributor
Posts: 282

Re: How can I read the filenames from a library into a dataset?

Hi,

Something similar was discussed in:

https://communities.sas.com/message/125021#125021

You can replace the put statement with an output statement or just use a sub-setting if statement.

Regards,

Amir.

Solution
‎08-13-2012 07:45 AM
Respected Advisor
Posts: 3,156

Re: How can I read the filenames from a library into a dataset?

Hi,

Since you mentioned 'library' instead of a folder/directory, I would assume you are trying to put data set names into a data set, and then there is a simple way: say you want every table names within 'work':

proc sql;

create table want as

select distinct memname as dname from dictionary.columns where libname='WORK';

QUIT;

Haikuo

Contributor
Posts: 53

Re: How can I read the filenames from a library into a dataset?

Hikuo!

I am getting an empty result :-(

Respected Advisor
Posts: 3,156

Re: How can I read the filenames from a library into a dataset?

Please post your code. Make sure your libname need to be CAPITAL. for instance, libname='work' will NOT work, it has to be libname='WORK'.

Haikuo

PROC Star
Posts: 7,471

Re: How can I read the filenames from a library into a dataset?

Haikuo's code should have worked.  What code did you submit and, if it was Haikuo's code literally, did you have any files in the WORK library?

If you changed the name of the libname, did you type the desired libname in all caps?

You also could have used dictionary.tables, but would get the same result.  e.g.:

proc sql;

  create table want as

    select memname

      from dictionary.tables

        where libname='WORK'

  ;

QUIT;

Contributor
Posts: 53

Re: How can I read the filenames from a library into a dataset?

I did not use CAPS.. that might be the reason. I figured out a different way now though... thx!

Respected Advisor
Posts: 3,156

Re: How can I read the filenames from a library into a dataset?

"I figured out a different way now though",

Care sharing with the forum?

Thanks,

Haikuo

Contributor
Posts: 53

Re: How can I read the filenames from a library into a dataset?

Sure! I added another filter.. and this code might be slightly slower ;-)

*-----------------------------------------------------------------------------+

|                     Daten aus dem Verzeichnis lls_wrk auslesen              |

*-----------------------------------------------------------------------------;

proc contents directory data=lls_wrk._all_ out=temp short;

run;

*-----------------------------------------------------------------------------+

|                         relevante Tabellennamen filtern                     |

*-----------------------------------------------------------------------------;

proc sql;

      create table lls_wrk as

            select distinct memname

            from temp

            where substr(memname, 1, 2) = "P_";

quit;


Super Contributor
Posts: 1,636

Re: How can I read the filenames from a library into a dataset?

you could add the filter to Art's code:

proc sql;

  create table want as

    select memname

      from dictionary.tables

        where libname='WORK' and substr(memname, 1, 2) = "P_";

quit;

Frequent Contributor
Posts: 129

Re: How can I read the filenames from a library into a dataset?

Based on your example code (appears you are referencing library referenced by LLS_WRK), you would need one additional change to Linlin's modification of  Art's code to make work:

proc sql;

  create table want as

    select memname

      from dictionary.tables

        where libname= "LLS_WRK" and substr(memname, 1, 2) = "P_";

quit;

Your example works just as well at the usage of dictionary.tables for your case.  but using dictionary views can be more powerful for other things.

Valued Guide
Posts: 2,177

Re: How can I read the filenames from a library into a dataset?

Posted in reply to LarryWorley

i'm recommending proc sql with sashelp.vmember rather than proc contents, dictionary.columns or dictionary.tables because it performs much better with non-standard libraries

🔒 This topic is solved and locked.

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

Discussion stats
  • 11 replies
  • 395 views
  • 4 likes
  • 7 in conversation