Help using Base SAS procedures

Capture File Date in INFILE

Reply
Occasional Contributor
Posts: 5

Capture File Date in INFILE

How can I capture the file date of the input file?  For example, my INFILE statement reads a file named Mytext.txt, and I want to capture the date that Mytext.txt was created, and add that date as a field in my dataset.

Super User
Posts: 10,497

Re: Capture File Date in INFILE

You won't do it as part of an INFILE statement. There are separate functions to examine external files. The specific one you want is FINFO but use is somewhat operating system dependent.

An example from the online help to read all of the file information for a given file <replace physical-filename with the path and file name:

data info;

  length infoname infoval $60;

  drop rc fid infonum i close;

  rc=filename('abc','physical-filename');

  fid=fopen('abc');

  infonum=foptnum(fid);

  do i=1 to infonum;

  infoname=foptname(fid,i);

  infoval=finfo(fid,infoname);

  output;

  end;

  close=fclose(fid);

run;

Once you know which value for infoname in the example you could ask for in in your program (once and retain the value).

Super User
Super User
Posts: 7,401

Re: Capture File Date in INFILE

My question would be, why would you want to do that?  The file date/time is dependant on several other things, so is not really a trustworthy source of information.  Personally I would deal with this either:

- By having this as part of the file transfer documentation.

- Using a format which allows metadata within the file, i.e. XML, or as a column for others dataset or CSV.

- Use a version control system, i.e. commit a file when it is received, then check the date/time from the VCS system as that cannot be altered.

I suspect you are trying to acertain when a file was modified etc, so this reason I caution the use of file dates.

Occasional Contributor
Posts: 5

Re: Capture File Date in INFILE

First off, I'm new to SAS and just learning, so I'm not sure exactly what you're talking about.  Sorry.

But here's the situatiion.  I will be receiving 30-40 different files periodically.  As new versions/generations of the files are received, I will use SAS to read the files, extract the fields I need, and consolidate the data into several different tables.  When I look at these tables, I want to be able to determine when I received the source file.

So, for example, if I'm receiving a text file MyText.txt monthly, when I look at my table, I want to be able to determine if the data in the table is from a file I received in April, or if it's March data.

Thanks,

Super User
Super User
Posts: 7,401

Re: Capture File Date in INFILE

Well, to read a file's date/time I say is a dangerous game. Especially on Windows.  Change your clock for instance then modify the file, you will see a wrong date.  My point is that instead of relying on Windows guessing, make it explicit that *you* create the date somehow.  There are numerous methods and it depends on your scenario:

- My preference if it is data is to have a data load to a database/warehouse.  These tools are built to load data, keep audit trail, roll-backs etc.

- Have a version control system, e.g. SVN.  It sounds like your files are text, so recieve the file, save it, commit it to SVN.  Then the date it is committed cannot be changed by any means, hence you can be sure the date is correct.

- Do manual version control, e.g. put each new data file in its own dated directory, e.g Data/20150601.  Then, by using the date of the directory you can be more sure of the actual date.  Not as confident as the above two however.

Its basically a process of Extract/Transform/Load: https://en.wikipedia.org/wiki/Extract,_transform,_load

But the point to take away is "don't rely on the file systems date/time value".

Super User
Posts: 10,497

Re: Capture File Date in INFILE

Not as robust as some other options but the date the file was actually read might work. If you are actually reading the data file with input statements then you could add:

DateRead = today();

You don't mention how you are bringing the data into SAS and whether you will be appending the data files at any point.

Super User
Posts: 17,819

Re: Capture File Date in INFILE

jhrbanker wrote:

So, for example, if I'm receiving a text file MyText.txt monthly, when I look at my table, I want to be able to determine if the data in the table is from a file I received in April, or if it's March data.

Thanks,

How do you know if a file you received is in April/March? Do you look at the create date of the file? What happens if you have a drive failure or a file gets recopied for some reason and the create date changes? Or is the date included in the file name?

Ask a Question
Discussion stats
  • 6 replies
  • 312 views
  • 0 likes
  • 4 in conversation