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

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.

1 ACCEPTED SOLUTION

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

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

5 REPLIES 5
pearsoninst
Pyrite | Level 9

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

 

Proc contents data = work._ALL_ nods out= abc;
run;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

slchen
Lapis Lazuli | Level 10

 

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

FreelanceReinh
Jade | Level 19

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;

 

FK
Calcite | Level 5 FK
Calcite | Level 5

Thank's guys for helping me out!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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