Help using Base SAS procedures

How to use SAS/SQL to count the number of EXCEL files

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

How to use SAS/SQL to count the number of EXCEL files

Suppose there are multiple EXCEL files under a file path. All the files have irregular names. Each file has multiple sheets, also irregularly named.      Now I want to use PROC SQL to count the number of EXCEL files under the file path, and the number of sheets in each different EXCEL file.    How to do it? Thank you so much!

 

Waiting for the responses~~~


Accepted Solutions
Solution
‎02-02-2016 11:17 PM
Super User
Posts: 3,250

Re: How to use SAS/SQL to count the number of EXCEL files

If you have the SAS product SAS/ACCESS to PC Files then it is possible to set up a LIBNAME to each Excel workbook and count up the number of sheets using the SAS DICTIONARY table MEMBERS.

 

It would take quite a bit of coding to get it working and it would ignore sheet names longer than 32 characters because that is the maximum SAS can handle via the DICTIONARY mechanism. I would probably take quite a while to run also if you have a lot of workbooks.

View solution in original post


All Replies
Super User
Super User
Posts: 7,942

Re: How to use SAS/SQL to count the number of EXCEL files

You can't directly, SAS only looks at SAS related files.  However you can query the underlying Operating System with its commands, and feed the results back into SAS. 

filename mylist pipe 'dir "c:\test\*.*" /b';

data have;

  infile mylist;

  length buffer $2000;

  input buffer;

run;

 

You can then count the observations of dataset have to get number of files.  The filename is a dos command - dir - with a location paramter and /b means basic info - i.e. just a filename.

Solution
‎02-02-2016 11:17 PM
Super User
Posts: 3,250

Re: How to use SAS/SQL to count the number of EXCEL files

If you have the SAS product SAS/ACCESS to PC Files then it is possible to set up a LIBNAME to each Excel workbook and count up the number of sheets using the SAS DICTIONARY table MEMBERS.

 

It would take quite a bit of coding to get it working and it would ignore sheet names longer than 32 characters because that is the maximum SAS can handle via the DICTIONARY mechanism. I would probably take quite a while to run also if you have a lot of workbooks.

Respected Advisor
Posts: 4,919

Re: How to use SAS/SQL to count the number of EXCEL files

I tried the following

 

%let path=&sasforum.\datasets;

filename mylist pipe "dir ""&path.\*.xls*"" /b";

data _null_;
  infile mylist truncover;
  length buffer $2000;
  input buffer $2000.;
  i+1;
  length file line $1024;
  file = quote(catx("\", "&path.", buffer));
  lib_name = cats("xl_", i);
  line = catx(" ", "libname", lib_name, "Excel", file, " access=readonly;");
  *put line;
  call execute(line);
run;

and it stopped at libref=XL_64, giving errors after that. So it looks like you have to extract the sheet names one workbook at a time.

PG
Super User
Super User
Posts: 7,942

Re: How to use SAS/SQL to count the number of EXCEL files

Hi,

 

Sorry, I didn't read that about Sheet names.  I would suggest the easiest way to do this would be to use VBA in Excel - use the right tech for the task.  It is a simple bit of code, outside of the scope of this forum however.  But google for:

Open list of files in Excel

http://software-solutions-online.com/2014/03/05/list-files-and-folders-in-a-directory/

 

Then simply loop over that list;

for each cell in range[...]

  open file

  cell.offset(0,1).value=activeworkbook.sheets

  close file

close

 

You then have a list of files, with number of sheets.  You could also output a list of sheet names if you.  VBA is pretty powerfull when using Excel.  Just google anything VBA excel <your topic> and there is lots of code available.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 483 views
  • 5 likes
  • 4 in conversation