BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
deblee73
Calcite | Level 5

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

4 REPLIES 4
Reeza
Super User
How are you importing the files/sheets?Add it in that step.
deblee73
Calcite | Level 5

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:

%macro import_file(path, file_name, dataset_name );
proc import 
datafile="&path.\&file_name."
dbms=xlsx
out=&dataset_name;
run;
 
%mend;
 
%*Call macro once for each entry in the list table created from the %list_files() macro;
data _null_;
set list;
string = catt('%import_file(', dir, ', ',  name,', ', compress(substr(the_name,1,15)), ');');
call execute (string);
run;
 
 
Not sure where to put it in the above.  Honestly, it was taking a lot of google code until I got something to work and this is the last piece.
I honestly don't need the stop have the file named the same, I just need to fine name in a column.  I found some come that named each file Test01, Test02 on import which is ok but it didn't put the file name in the file.  
Tom
Super User Tom
Super User

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;
deblee73
Calcite | Level 5

Sa-Weeeet.  It works.  God bless you. Thank you so very much.  

sas-innovate-white.png

Special offer for SAS Communities members

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.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1241 views
  • 2 likes
  • 3 in conversation