BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Emma2021
Quartz | Level 8
I have multiple folder on my C drive. Within each folder, I have one or two excel files. How can I find out all the names of folders and the excel files names?

I wanted to use a macro to read those excel files into sas -how can accomplish ? Thank you.
1 ACCEPTED SOLUTION

Accepted Solutions
A_Kh
Lapis Lazuli | Level 10

What @ballardw explained above might look like as below in code:

filename xlsxlist pipe 'dir /b /s "C:\Users\Myfolders\*.xlsx" ';
data temp;
   infile xlsxlist truncover;
   input file_path_name $200.;
   file_name= scan(file_path_name, -1, '\');
proc print;run;

Tested. Works fine for me. 

 

View solution in original post

6 REPLIES 6
ballardw
Super User

Easiest would be the Windows DIR command such as open a Windows command window and type:

Dir /S/B C:\*.xlsx

 

to create a TEXT file to hold that output add a ><path>\myfile.txt

at the end or similar:

Dir /S/B C:\*.xlsx > c:\folder\myfile.txt

 

The /S option on the command is search subfolders, so starting at C: it will search every folder on your drive. The /B option just lists the folder/file name information. The * is a wildcard for any file name with the given extension. If you have older XLS files as well as XLSX you would be best off by making two separate output files unless you are familiar with the Windows command line.

 

If your SAS is not server based you likely could use that as a PIPE in a filename but if your SAS is server based it likely doesn't see your hard drive to do such.

Emma2021
Quartz | Level 8
Thank you. I have the desktop version sas 9.4. Could you help with the sas code? Thanks again
Emma2021
Quartz | Level 8
Sorry what is “Dir”? My path is as follow C://documents/data/…
After this “data”, I have many sub folders with excel files
Reeza
Super User

Dir is a unix command. You can instead use a pre-written macro to get the list of files that you will then need to likely filter to what you want to import. Chantge the first path in the %dirtree below to be your path and run the program. Note that I've set it to run/search only to a depth of 10 folders deep. If you want it go further change the maxdepth parameter. It will create a table called filelist that will have a list of the files/folders. 

 

filename dirtree url 
    'https://raw.githubusercontent.com/sasutils/macros/master/dirtree.sas';
%include dirtree /source2;
%dirtree(C:\documents\data\   /* Pipe delimited directory list (default=.) */
        , out=filelist  /* Output dataset name */
        , maxdepth=10 /* Maximum tree depth */);

This will contain the list of all files and folders. You can then use a data step to filter out the files that only have the xlsx extension. 

 

data excel_file_list;
set filelist;
where type = 'F';
*test that this works,if it does uncomment the line below to filter only xlsx files;
ext = reverse(scan(reverse(filename), 1, "."));
*if ext = 'xlsx';
run;

@Emma2021 wrote:
Sorry what is “Dir”? My path is as follow C://documents/data/…
After this “data”, I have many sub folders with excel files



A_Kh
Lapis Lazuli | Level 10

What @ballardw explained above might look like as below in code:

filename xlsxlist pipe 'dir /b /s "C:\Users\Myfolders\*.xlsx" ';
data temp;
   infile xlsxlist truncover;
   input file_path_name $200.;
   file_name= scan(file_path_name, -1, '\');
proc print;run;

Tested. Works fine for me. 

 

ballardw
Super User

I would set the length of the variable to at least 256, which I think is the maximum path and filename that windows supports to avoid truncation of names.

 

Warning: if you have ONE-Drive installed, which typically places access in C:\users then you may be pulling names from cloud storage and not your local drive. I don't know about any other cloud services.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 1584 views
  • 5 likes
  • 4 in conversation