BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

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

2 REPLIES 2
Kurt_Bremser
Super User

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;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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...

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 405 views
  • 2 likes
  • 3 in conversation