Hello.
I need to import all excel files in a directory (all have very different names) and on import or after import, I need to add a column to each file and populate it with the file name. So if I have say 10 excel files and each have a different name of the month as the file name, I want to import them all and add a column "month" and populate it with the file name.
This works but it's only one file at a time.
data list;
set list indsname=name;
tablename=scan(name,2);
run;
So this code is assuming that each XLSX workbook only has ONE worksheet (or that you only want the first worksheet when it has more than one).
Just add the step to add the name into the macro.
%macro import_file(path, file_name, dataset_name );
proc import
datafile="&path.\&file_name."
dbms=xlsx
out=&dataset_name
;
run;
data &dataset_name;
set &dataset_name;
length file_name $100;
file_name="&file_name";
run;
%mend;
Not sure if you need to see all the code but the first big chunk gets a list of all the Excel files in the directory.
Then, the import:
So this code is assuming that each XLSX workbook only has ONE worksheet (or that you only want the first worksheet when it has more than one).
Just add the step to add the name into the macro.
%macro import_file(path, file_name, dataset_name );
proc import
datafile="&path.\&file_name."
dbms=xlsx
out=&dataset_name
;
run;
data &dataset_name;
set &dataset_name;
length file_name $100;
file_name="&file_name";
run;
%mend;
Sa-Weeeet. It works. God bless you. Thank you so very much.
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
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.
Ready to level-up your skills? Choose your own adventure.