Help using Base SAS procedures

PROC Import of Multiple Files

Reply
Super Contributor
Posts: 358

PROC Import of Multiple Files

Hi All:

I have used PROC IMPORT to input a single EXCEL spreadsheet with no problem. Someone has asked me how they can import 1,100 spreadsheets at once. They all have similar names and are all in the same directory. Is there a way to use either a wildcard (eg. 'C:\temp\file*.xls') of a FILEVAR to identify the input files? I know that I could use a macro but I'm just being lazy.

Thanks in advance.
N/A
Posts: 0

Re: PROC Import of Multiple Files

Hello lazy OS2 Rules,

Here are my 2 cents, I didn't use Excel file name as the import dataset name, because some file might start with numeric or contains space or longer than what SAS required, but you can always change what you want.

Cheer.


/***************************************************;
* Program: TestImportMultipleExcel.sas
* Programmer: Clement Chen, sdclement@gmail.com
* Date: 4/4/2009
****************************************************/;

options nomlogic mprint nosymbolgen;
%let indir=C:;
%let ftype=%str(*.xls);
%let idtext=%str(*.*);

filename dir pipe "command/c find /c /i %str(%"&idtext%") &indir\&ftype";

data fname;
infile dir truncover lrecl=256;
input fname0 $ 1-256;
length fname $256;
if fname0='' then delete;
fname0=compress(upcase(fname0),'-');
_len=length(scan(fname0,-1,':'))+1;
totlen=length(fname0) - _len;
fname=substr(fname0,1,totlen);
keep fname;
run;

proc sql noprint;
select put(count(*), 3.) into: TotalN
from fname;

select distinct(fname) into: fname1- :fname%left(&TotalN)
from fname;
quit;


%macro GetExcelFile();

%do i=1 %to &TotalN;

PROC IMPORT OUT= WORK.Excel&i
DATAFILE= "&&fname&i"
DBMS=EXCEL REPLACE;
GETNAMES=YES;
MIXED=NO;
SCANTEXT=YES;
USEDATE=YES;
SCANTIME=YES;
RUN;

%end;

%mend GetExcelFile;

%GetExcelFile(); Message was edited by: sdclement
Ask a Question
Discussion stats
  • 1 reply
  • 141 views
  • 0 likes
  • 2 in conversation