Import multiple excel files where in some dates files don't exist

Reply
Frequent Contributor
Posts: 95

Import multiple excel files where in some dates files don't exist

Hello

My task at work is to import multiple Excel files into SAS .

Each excel file that I import will be a SAS data set.

The Excel files are a daily files that started from 01012017(01Jan2017) until 31122017(31Dec2017).

I crrated a vector of dates from 01012017  until 31122017  and run a macro to import the excel files.

The problem is that there are dates that there is no excel file and then I get an error while I run the import Macro.

What do you offer to do?

Is there a way to tell sas that if it dosen't find the excel file then don't send an error and just create an empty file?

Is there a way to tell SAS  to import excel files only for existing dates?

 

 

thanks

Ron

Super User
Posts: 9,612

Re: Import multiple excel files where in some dates files don't exist

Use the fexist() function to determine if a file is present, and make your import step dependent on this.

eg

%macro your_macro;

/* filename is determined here */

data _null_;
if fexist("&filename")
then call symput('file_present','yes');
else call symput('file_present','no');
run;

%if &file_present = yes
%then %do;

/* import */

%end;
%else %do;

/* data step that creates an empty file with correct variables */

%end;

/* further processing */

%mend;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 9,227

Re: Import multiple excel files where in some dates files don't exist

Do you have command line access?  If so use a directory listing to only import those given files:

filename tmp pipe 'dir "c:/yourlocation/*.xlsx" /b';

data _null_;
  infile tmp;
  input;
  call execute(cats('%Import_XLSX (fname=',_infile_,');'));
run;

This will (on windows, unix is slightly different) call a directory listing and for each filename recieved call a macro call Import_XLSX with the file in question.  This way you don't need to know what is there (i.e. date ranges etc.).  You can do the same with SAS fopen and such like commands also.

 

The problem here really is the use of Excel and the Excel thinking behind it for instance none of this would be an issue if it wasn't for the need to put data in filenames...

Ask a Question
Discussion stats
  • 2 replies
  • 127 views
  • 2 likes
  • 3 in conversation