BookmarkSubscribeRSS Feed
anirudhs
Obsidian | Level 7

Hi looking for code which can check for the latest file based on its created date and time, if it find latest file then it should execute the process flow.

 

Hint: the file can be of any sas readable format.

 

eg. if a.xlx was last modified/created Today's date at 3:13 pm then it should import and execute.

if not updated then ...it should terminate the execution.

8 REPLIES 8
RW9
Diamond | Level 26 RW9
Diamond | Level 26

First, using file data/time is not a good way of doing any process - it can easily be modified.  

Second, define "SAS readable" as SAS can read almost any file type.  What are the specifications for the process, what do you have written down in the functional design definition for the process.  What agreement do you have on what data will be given?  Just saying you want to do something on anything that appears is not going to work, ever.  Clear process, clear documentation, fixed inputs, fixed outputs.

anirudhs
Obsidian | Level 7
i want to read .xlsx file with latest modified time.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Assuming your on Windows:

filename tmp pipe 'dir "yourdirectory/*.xlsx" /b /o-d';

data _null_;
  infile tmp dlm="¬";
  if _n_=1 then call execute('proc import datafile='||strip(_infile_)||' out=want; run;');
run;

This will use DOS to return a list of files sorted by date reversed, then on first one generate a proc import statement.

 

That being said, your code will fall over each run, the process is way to vague, Excel is a poor data medium.  What happens if they post a file called xlsx but its not, or not in the right form, or don't post the file, or put a picture in it...

anirudhs
Obsidian | Level 7
Yes im on windows and surely i will try for the same.
s_lassen
Meteorite | Level 14

This code will read a directory and find the creation times for .xlsx files:

filename indir '<directory>';
data test;
  dirid=dopen('indir');
  do filenum=1 to dnum(dirid);
    name=dread(dirid,filenum);
    if upcase(scan(name,-1,'.')) ne 'XLSX' then
      continue;
    fileid=mopen(dirid,name);
    if fileid=0 then do;
      put _all_;
      continue;
      end;
    Created=finfo(fileid,'Create Time');
    output;
    rc=fclose(fileid);
    end;
  keep name created;
run;

The created variable will be a datetime, but formatted according to your national language settings in Windows, you then have to find the correct informat for reading it into a numeric datetime. After that, it should be as simple as to sort by the date and take the last record.

anirudhs
Obsidian | Level 7
i dont want the last record by date or time .... i want to read the file only when it is modified where the windows updates the date and time when the file is modified ..so if the file is not modified then it should not read it.
error_prone
Barite | Level 11
So you need a datetime with the last run of the process to have something to compare the files modification date to. Where do you store that information?
anirudhs
Obsidian | Level 7
No sir,
when i will hit the run in EG to run the code for importing the .xlsx file then at that time if the xlsx file in the derictory has modified date/time then it should import. If it finds the last run time/date then it should not run ..it should terminate with msg : old file.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1996 views
  • 0 likes
  • 4 in conversation