DATA Step, Macro, Functions and more

Filename - based on date file was created

Accepted Solution Solved
Reply
Contributor
Posts: 46
Accepted Solution

Filename - based on date file was created

[ Edited ]

I'm trying to automate a program that reads in EXCEL files.  Currently I have to browse to the Windows directory to see what files have been added, then I copy/paste into my program.

 

I want to scan the Windows Folder and find the files with yesterdays date dynamically to create my filename statement.

 

Is that possible?

 

Thanks,

Tammy


Accepted Solutions
Solution
‎03-09-2018 01:27 PM
PROC Star
Posts: 631

Re: Filename - based on date file was created

Hi,

 

You can run x-commands to read all the files in a folder. Does your excel files have datestamp on files names? If yes and you don't have access to run x-commands then the following code can read the files in a specific folder.

FILENAME _folder_ "%bquote(C:\Program Files\test)";
data filenames(keep=memname);
  handle=dopen( '_folder_' );
  if handle > 0 then do;
    count=dnum(handle);
    do i=1 to count;
      memname=dread(handle,i);
      output filenames;
    end;
  end;
  rc=dclose(handle);
run;
filename _folder_ clear;

 

Thanks,
Suryakiran

View solution in original post


All Replies
Super User
Posts: 5,917

Re: Filename - based on date file was created

Yes, there are multiple ways to do it.

You could either use SAS external file functions to find out directory contents, files and their creation dates (FINFO).

Or, have an OS script prepare that information for you, import it to SAS, and use macro coding to generate FILEAME based on that.

Data never sleeps
Solution
‎03-09-2018 01:27 PM
PROC Star
Posts: 631

Re: Filename - based on date file was created

Hi,

 

You can run x-commands to read all the files in a folder. Does your excel files have datestamp on files names? If yes and you don't have access to run x-commands then the following code can read the files in a specific folder.

FILENAME _folder_ "%bquote(C:\Program Files\test)";
data filenames(keep=memname);
  handle=dopen( '_folder_' );
  if handle > 0 then do;
    count=dnum(handle);
    do i=1 to count;
      memname=dread(handle,i);
      output filenames;
    end;
  end;
  rc=dclose(handle);
run;
filename _folder_ clear;

 

Thanks,
Suryakiran
Contributor
Posts: 46

Re: Filename - based on date file was created

Posted in reply to SuryaKiran

This works great -what is the command to find the file create date so I only get the files  I need each day?  Or if you can point me to documentation I would be most grateful.

 

Thank you

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 277 views
  • 0 likes
  • 3 in conversation