DATA Step, Macro, Functions and more

Names of datasets in a library to write into a dataset

Accepted Solution Solved
Reply
Occasional Contributor FK
Occasional Contributor
Posts: 5
Accepted Solution

Names of datasets in a library to write into a dataset

Hello Everyone,
 
I do have a question regarding the folowing subject:
 
Assume you have the following datasets in your work library:  "customers" , "costs" , "revenues" and  "transactions".
 
How can I  efficiently   write a macro which generates one dataset called "overview" with a variable named "datasetnames" in which the corresponding name of each of the dataset in my work library is written into.
 
So in the described case above the "overview" dataset should look like this:
 
 
     datasetnames
1    customers
2    costs
3    revenues
4    transactions
 
Thank you in advance for any help in this topic.


Accepted Solutions
Solution
‎11-15-2015 01:15 PM
Super User
Super User
Posts: 7,401

Re: Names of datasets in a library to write into a dataset

For what purpose?  SAS has a set of metadata tables in SASHELP library which tell it what libraries, datasets, columns etc. are used.  Just query that:

data want;

  set sashelp.vtable (where=(libname="WORK") keep=memname);

run;

View solution in original post


All Replies
Frequent Contributor
Posts: 108

Re: Names of datasets in a library to write into a dataset

I think you dont have to write macro..try the below codes;

 

Proc contents data = work._ALL_ nods out= abc;
run;
Solution
‎11-15-2015 01:15 PM
Super User
Super User
Posts: 7,401

Re: Names of datasets in a library to write into a dataset

For what purpose?  SAS has a set of metadata tables in SASHELP library which tell it what libraries, datasets, columns etc. are used.  Just query that:

data want;

  set sashelp.vtable (where=(libname="WORK") keep=memname);

run;

Super Contributor
Posts: 275

Re: Names of datasets in a library to write into a dataset

 

proc sql;
create table dataname as
select memname as datasetnames from dictionary.columns
where libname='WORK';
quit;

Trusted Advisor
Posts: 1,115

Re: Names of datasets in a library to write into a dataset

If you really want a macro to perform this task, you could try something like this:

 

%macro listds(lib=work, out=overview, namevar=datasetnames, order=memname);
  proc sql;
  create table &out as
  select memname as &namevar
  from dictionary.tables
  where libname="%upcase(&lib)" & memtype='DATA'
  order by ℴ
  quit;
%mend listds;

Here you have the flexibility of specifying

  • the library you're interested in (parameter lib, default: work)
  • the name of the output dataset (parameter out, default: overview)
  • the name of the variable containing the dataset names (parameter namevar, default: datasetnames)
  • the characteristic by which the dataset names are ordered (parameter order, default: memname [i.e. the dataset name]; other options include crdate [i.e. creation date], modate [i.e. last modification date], or filesize).

Sample calls:

%listds;  /* gives what you have described, however sorted alphabetically by dataset name */
%listds(lib=mylib, out=newlib.dslist, namevar=dsname, order=crdate) /* assuming that libraries mylib and newlib exist */

For more options regarding parameter order see the description of dictionary.tables:

proc sql;
describe table dictionary.tables;
quit;

 

Occasional Contributor FK
Occasional Contributor
Posts: 5

Re: Names of datasets in a library to write into a dataset

Thank's guys for helping me out!

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 244 views
  • 0 likes
  • 5 in conversation