DATA Step, Macro, Functions and more

re: retrieving variable attributes

Regular Contributor
Posts: 248

re: retrieving variable attributes

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.

Trusted Advisor
Posts: 1,848

Re: re: retrieving variable attributes

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 */

or use 

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

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.




Super User
Posts: 24,026

Re: re: retrieving variable attributes

Do you have text files or SAS datasets?


Super User
Posts: 6,935

Re: re: retrieving variable attributes

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


proc contents data=lib._all_;


Super User
Super User
Posts: 8,287

Re: re: retrieving variable attributes

[ Edited ]

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 @;
  if _n_=1 or eov then do column=1 by 1 until( cc > len) ;
    input varname @ ;


Ask a Question
Discussion stats
  • 4 replies
  • 5 in conversation