BookmarkSubscribeRSS Feed
kjarvis
Fluorite | Level 6

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;

 

2 REPLIES 2
Reeza
Super User

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.

https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-import-multiple-tex...

 

 

 

 

art297
Opal | Level 21

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

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!

How to Concatenate Values

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.

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
  • 2 replies
  • 1758 views
  • 3 likes
  • 3 in conversation