03-10-2014 10:05 AM
I was hoping somebody would have a simple solution to this.
I have a directory that has a large number of files in it. The first record of each file has a date on it, with the data following.
I need to read all the records from the directory for a specific month (and year).
This would mean that I have to read all files in the directory, and keep the name of the file. Then I would read the first line
to determine the date - if it is not the month that I was, I reject the file and read the next one.
If the file is within my date rage, I would keep the file name. Once I have examined all files, I would then re-process just
the file that I retained.
This seems like a "brute force" approach and I was hoping for something a little more elegant - any ideas?
Thanks in advance.
03-10-2014 10:24 AM
Brute force is the way to go, but doing via a program.
Get the list of file names. I usually find this is easier with PIPEd operating system commands. DIR /B in Windoze and ls in Unix.
You will need to supply input statement for reading the date out of the first line as the syntax will depend on how it is formatted.
%let month=01JAN2014 ;
data files ;
infile "ls &mydir/*" pipe truncover ;
input filename $255. ;
filen = filename ;
infile dummy filevar=filen truncover end=eof ;
if not eof then input date date9. @ ;
if date = "&month"d then output;
03-12-2014 09:19 AM
Thanks for the help - it looks like brute force is the only way to go.
Another question - is there a way for me to read only a single record from the file in a data step without reading the rest of the data? Some of these files are somewhat large and I only need the header.
Currently I use something like:
infile myfile lrecl=100 pad missover;
input @001 text $char51.;
if _n_ = 1 then output;
I would rather not have to read the entire file if that is possible (option obs=1 possibly?)
03-12-2014 09:25 AM
The code I posted before would only read one record per file because the main loop was the list of files.
If you are reading a single file then just add the option OBS=1 to the INFILE statement.
infile myfile lrecl=100 obs=1 truncover ;
input text $char51.;
03-12-2014 10:19 AM
Assuming that your files have a particular date structure, then you can get a list of files you want to process quite simply with a DOS command.
findstr "..MAY2013" *.txt
filename rob pipe 'findstr /m "..MAY2013" "s:\temp\rob\*.txt"';
attrib buffer format=$2000.;
infile rob lrecl=30000 dsd;
Will give me a dataset called dir which contains the path and filename of any files with .txt extension in s:\temp\rob which have a pattern --MAY2013. I can then read those files in. This means you wouldn't have to read each file, but does mean the data needs to be structured.
03-12-2014 10:23 AM
Unfortunately, the date is in the header of the data, not in the file name. I also have to compare this to the date the file was created to determine if the file was rerun more than once, and keep the latest one only.