Help using Base SAS procedures

Get the max date of a file in the directory

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 80
Accepted Solution

Get the max date of a file in the directory

Hi everyone,

Any help will be very grateful.

I need to get the most updated file from a directory to infile as data set;

ex:

     in the directory HAVE i have the following files

     HAVE201310.TXT

     HAVE201311.TXT

     HAVE201312.TXT

     HAVE201309.TXT

I WANT infile only the HAVE201312.TXT file (is the most updated file)

Thanks in advance.

Augusto Souza


Accepted Solutions
Solution
‎11-12-2013 02:33 PM
Super User
Posts: 5,071

Re: Get the max date of a file in the directory

Well, I'm picturing a solution that issues an operating system command to obtain a list of the file names.  For example, in Unix a SAS program might contain this statement:

%sysexec ls /folder/path/have*.txt > /folder/path/list_of_have_files.txt;

That would create a file holding a list of all the files that you need to search.  It would be a simple matter to read those into a SAS data set, and extract the latest name:

data have_files;

   infile "/folder/path/list_of_have_files.txt";

   length path $200;

   input path $ &;

run;

proc sort data=have_files;

   by descending path;

run;

data _null_;

   set have_files (obs=1);

   call symputx('file_i_want', path);

run;

But the operating system command to get a list of files would vary by operating system.  So here's the Unix example ... I expect you would use a "dir" command under Windows.

Good luck.

View solution in original post


All Replies
Super User
Posts: 5,071

Re: Get the max date of a file in the directory

What operating system? 

Frequent Contributor
Posts: 80

Re: Get the max date of a file in the directory

Sorry Astounding, it will be used in Unix and also Windows. But what is the difference? Fow now i'm just using Unix (its more urgency).

Solution
‎11-12-2013 02:33 PM
Super User
Posts: 5,071

Re: Get the max date of a file in the directory

Well, I'm picturing a solution that issues an operating system command to obtain a list of the file names.  For example, in Unix a SAS program might contain this statement:

%sysexec ls /folder/path/have*.txt > /folder/path/list_of_have_files.txt;

That would create a file holding a list of all the files that you need to search.  It would be a simple matter to read those into a SAS data set, and extract the latest name:

data have_files;

   infile "/folder/path/list_of_have_files.txt";

   length path $200;

   input path $ &;

run;

proc sort data=have_files;

   by descending path;

run;

data _null_;

   set have_files (obs=1);

   call symputx('file_i_want', path);

run;

But the operating system command to get a list of files would vary by operating system.  So here's the Unix example ... I expect you would use a "dir" command under Windows.

Good luck.

Frequent Contributor
Posts: 80

Re: Get the max date of a file in the directory

Hi Astounding... it worked perfectly...thanks to much for your help.

Super User
Super User
Posts: 6,497

Re: Get the max date of a file in the directory

To find the most recent file on Unix you can use:

data _null_;

  infile 'ls -t | head -1' pipe ;

  input;

  call symputx('lastfile',_infile_);

run;


To find the most recent file on Windows you can use:

data _null_;

  infile 'dir /od /b' pipe end=eof;

  input;

  if eof then call symputx('lastfile',_infile_);

run;

Super Contributor
Posts: 1,636

Re: Get the max date of a file in the directory

or

data _null_;

  infile 'dir C:\TEMP\forum\test/o-d /b' pipe obs=1;

  input ;

   call symputx('last',_infile_);

run;

Contributor
Posts: 41

Re: Get the max date of a file in the directory

Hi I have same issue but I have files in SAS library/folder how can i use this code. pls help me

Super User
Posts: 5,071

Re: Get the max date of a file in the directory

Tom, Linlin,

That's definitely elegant code, worthy of being noted.  That being said, I have a bias toward putting the list into a file, rather than piping it in, for two reasons.  The list serves as documentation as to what files were available when the program ran, and it can help in debugging if the results are not as expected.  Usually I extend the logic so that the list automatically follows the program around.  For example, if the program is prog1.sas, the list might be saved as prog1.have_filelist.  It's not too much work for macro language to capture the program name and automatically create a matching output file. Shameless plug time:  I wrote up details in chapter 13 of the SAS Press book SAS Macro Language Magic.  Thanks for adding to my knowledge base.

Super User
Super User
Posts: 6,497

Re: Get the max date of a file in the directory

The big problem with using %SYSEXEC or X commands is that it is much more likely that any unexpected error messages from the command are going to be lost.  If instead you nest the operating system command into a data step you can get control of those messages.

If you want to create a file from the input of a piped operating system command then you can use SAS code for that also.

data _null_;

   infile 'dir' pipe ;

   file 'dir.list' ;

   input ;

   put _infile_;

run;

Frequent Contributor
Posts: 80

Re: Get the max date of a file in the directory

Thanks all for participating.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 1700 views
  • 7 likes
  • 5 in conversation