Hi,
I have a number of Excel files, named Company1_2016Q1.xlsx, Company1_2016Q2.xlsx, Company2_2016Q1.xlsx, Company2_2016Q2.xlsx etc. They are stored in one folder with multiple subfolders.
I have a following macro to import all Excel files from a catalog to SAS (I've found it somwhere on the Web, I didn't write it on my own). Could you help me modify the macro in two ways:
1. I would like to loop not only through the folder but also through subsoflders and sub-subfolders (if they exist) 2. I have some problem with manipulating the imported datasest - please take a look at the comments in the code below.
%macro read_files (katalog);
%let rc=%sysfunc(filename(rawdata,&katalog));
%let did=%sysfunc(dopen(&rawdata));
%let dnum=%sysfunc(dnum(&did));
%do i=1 %to &dnum;
%let name_file=%sysfunc(dread(&did,&i));
%let rozszerz=%sysfunc(scan(&name_file,2,.));
%if &rozszerz=xlsx %then %do;
%let name_file_short=%sysfunc(scan(&name_file,1,.));
PROC IMPORT OUT=WORK.&name_file_short
DATAFILE="&katalog\&name_file" DBMS=xlsx REPLACE;
RUN;
/* HERE IS THE PROBLEM:
- I want to create two variables in the dataset on the basis of the dataset name but the substr doesn't work*/
data WORK.&name_file_short;
set WORK.&name_file_short;
comp=%substr(&name_file_short,1,8);
date=%substr(&name_file_short,10,6);
run;
%end;
%end;
%mend;
%read_files(C:\Data);
... View more