Hello,
How can I read the file date from an external file in SAS?
I have a job that is already reading the fline name to load in multiple external files daily – I would like to include the date of the file in the data set I create.
For example if the file was created on 3/27/2017 - I would like the code to read in 3/27/2017 so I can add it to my data set.
I’m using User Written code in DI Studio to read in the file name – but I can't find how to read in the file date. Here’s the code I have so far:
--------------------------------------------------------
filename DIRLIST pipe 'dir "\\filelocation\*.csv" /b ';
data dirlist ;
infile dirlist lrecl=200 truncover;
input InFile_name $100.;
run;
data _null_;
set dirlist end=end;
count+1;
call symputx('read'||put(count,7.-l),cats('filelocation',InFile_name));
call symputx('dset'||put(count,7.-l),scan(InFile_name,1,'.'));
if end then call symputx('max',count);
run;
options mprint symbolgen;
%macro readin;
%do i=1 %to &max;
data &&dset&i;
infile "&&read&i" lrecl=1000 truncover dsd;
input var1 $ var2 $ var3 $;
run;
%end;
%mend readin;
libname BICTMPOD '\\filelocation\TEMPODBC';
data BICTMPOD.work_In_File_Names;
set dirlist;
run;
In the DIR option make sure you specify the correct options to include the created/modified date in the output. Then in the data step you read in the variables needed.
Windows reference for DIR statement.
https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/dir.mspx?mfr=true
And here's a SAS note that illustrates how to do this:
http://support.sas.com/kb/38/267.html
You do know that you can read all text files at once if they have the same structure, if they're in the same folder use a wildcard.
The following works for me (using Windows10):
filename DIRLIST pipe 'dir "c:\art\*.csv" /tw '; data dirlist (keep=InFile_name date); informat date yymmdd10.; format date date9.; informat time time8.; format time time5.; informat size comma12.; informat InFile_name $100.; infile dirlist lrecl=200 truncover; input date time ampm $ size InFile_name; if not missing(date); run;
Art, CEO, AnalystFinder.com
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.