BookmarkSubscribeRSS Feed
twildone
Pyrite | Level 9

Hi...I have about 100 text files stored in a directory that vary in size and in the number variables/fields within each text file. The names of each text file vary with no common pattern to the names. I would like to extract information about each field within each text file such as Field name, type, length, etc similiar to the results obtained from using PROC  CONTENT. Is there an efficient way to do this without having to run the PROC CONTENT procedure 100 times? Thanks.

4 REPLIES 4
Shmuel
Garnet | Level 18

Using term "text file" with "proc contents" means yo have a library (=directory) with sas datasets.

 

You can use either:

proc sql;
   select * from distionary.columns
   where libname ='LIBNAME'   ;  /* replace to your libname in capital leters */
quit;

or use 

data contents;
   set sashelp.columns(where=(libname='LIBNAME'));
run;

each of them is equivalent to run proc contents on all datasets in that library.

Tha names of variables in output may differ between the two methods.

 

 

 

Reeza
Super User

Do you have text files or SAS datasets?

 

Astounding
PROC Star

If you have many SAS data sets within a single library, you can simply use:

 

proc contents data=lib._all_;

run;

Tom
Super User Tom
Super User

Text files do not normally have information on type or length.  Typically text files are either in some previously defined format or perhaps they are CSV or other delimited files in which case the only metadata they have is the variable names in the first line.  

 

How are currently converting those files into datasets?

 

If they really are delimited text files then perhaps you could write code to read the header lines to find the variable names and position?

data varnames;
  length filename filen $200 column 8 varname $32 ;
  infile 'my directory/*' dsd filename=filen eov=eov column=cc length=len truncover;
  input @;
  filename=filen;
  if _n_=1 or eov then do column=1 by 1 until( cc > len) ;
    input varname @ ;
    output;
  end;
  eov=0;
run;

 

sas-innovate-2024.png

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.

 

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