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
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;
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...
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.