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
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.
What operating system?
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).
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.
Hi Astounding... it worked perfectly...thanks to much for your help.
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;
or
data _null_;
infile 'dir C:\TEMP\forum\test/o-d /b' pipe obs=1;
input ;
call symputx('last',_infile_);
run;
Hi I have same issue but I have files in SAS library/folder how can i use this code. pls help me
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.
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;
Thanks all for participating.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.