BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Augusto
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

10 REPLIES 10
Astounding
PROC Star

What operating system? 

Augusto
Obsidian | Level 7

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).

Astounding
PROC Star

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.

Augusto
Obsidian | Level 7

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

Tom
Super User Tom
Super User

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;

Linlin
Lapis Lazuli | Level 10

or

data _null_;

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

  input ;

   call symputx('last',_infile_);

run;

chirumalla
Calcite | Level 5

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

Astounding
PROC Star

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.

Tom
Super User Tom
Super User

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;

Augusto
Obsidian | Level 7

Thanks all for participating.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 10 replies
  • 5176 views
  • 7 likes
  • 5 in conversation