01-03-2018 02:53 AM
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?
01-03-2018 03:30 AM
Use the fexist() function to determine if a file is present, and make your import step dependent on this.
%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;
01-03-2018 04:36 AM
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...