BookmarkSubscribeRSS Feed
Q1983
Lapis Lazuli | Level 10

Lets say I have a libname called myfiles with the contents as follows:

acct_cd_20210401

acct_cd_20210412

cof_fod_20200419

cof_fod_20220411

mrt_ftr_20200402

mrt_ftr_20220418

 

I want to do a proc contents on the last dataset in each category by name and date only (ie acct_cd_20210412,cof_fod_20220411,mrt_ftr_20220418)

proc contents data=myfiles. ????????

I want to get the names of fields in the tables however I do not want to have to do a proc contents on each and every table name.  Can such a query be done?

3 REPLIES 3
Reeza
Super User

Look at the SASHELP.VCOLUMN or dictionary.column table which contains the metadata for tables. 

You can filter by the library easily. There is also a creation date but you'll need to add a column to separate your date into the date portion and prefix of the table name and then select only the records of interest. 

 


@Q1983 wrote:

Lets say I have a libname called myfiles with the contents as follows:

acct_cd_20210401

acct_cd_20210412

cof_fod_20200419

cof_fod_20220411

mrt_ftr_20200402

mrt_ftr_20220418

 

I want to do a proc contents on the last dataset in each category by name and date only (ie acct_cd_20210412,cof_fod_20220411,mrt_ftr_20220418)

proc contents data=myfiles. ????????

I want to get the names of fields in the tables however I do not want to have to do a proc contents on each and every table name.  Can such a query be done?


 

 

ballardw
Super User

Last by which date? The date in the name? The creation date? The modified date?

 

Perhaps you might find this code of interest. Replace 'WORK' with the name of your Library in upper case. This creates a table with an X at the intersection of variable name and type and data set name. It is often useful to know that the same named variable occurs as both character and numeric. If you really want to you could add the other characteristics of variables such as Label, format or length but that may be disheartening if find lots of differences for same-named variables.

proc format;
value x
.=' '
other='X'
;
run;
proc tabulate data=sashelp.vcolumn;
   where libname='WORK' and memtype='DATA';
   class name memname type;
   table name*type,
         memname*n=' '*f=x.
         /misstext=' '
   ;
run;
Tom
Super User Tom
Super User

Get this list of MEMBERs in the library.  Strip off the numeric suffix.  Sort by the remaining text.  Take the last (or first) one.

Let's test it.  First let's make some datasets with those names.

data acct_cd_20210401 acct_cd_20210412 cof_fod_20200419 cof_fod_20220411 mrt_ftr_20200402 mrt_ftr_20220418;
  set sashelp.class(obs=1);
run;

Now make the ordered list.

proc sql ;
create table list as 
  select catx('.',libname,memname) as dsname
       , substr(memname,1,length(memname)-8) as basename
       , scan(memname,-1,'_') as suffix
  from dictionary.members 
  where libname='WORK'
    and not missing(input(scan(memname,-1,'_'),?yymmdd8.))
  order by 2,3
;
quit;

Now pick the last (latest) one for each basename and generate PROC CONTENT code.

data _null_;
  set list;
  by basename;
  if last.basename;
  call execute(catx(' ','proc contents data=',dsname,';run;'));
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 314 views
  • 0 likes
  • 4 in conversation