BookmarkSubscribeRSS Feed
Ayisha
Calcite | Level 5

How to check names of excel sheets in order?

 

I used proc datasets lib= option 

but the sheet names appear in alphabetical order.

How to get the names in the same order as they appear in excel sheet?

4 REPLIES 4
Patrick
Opal | Level 21

I believe this information doesn't get read into SAS. Given that you could also read ranges instead of sheets and that there could be more than one range per sheet (=multiple SAS tables reading data from the same sheet) I also don't see how SAS could reasonably store such information.

If you really need the order of the sheets within the Excel then I guess you would need some VB script (or similar) which extracts the information and then read this into a separate SAS table. What's possible depends on the OS of your SAS server and if you can issue OS level commands (=option XCMD must be set).

Ksharp
Super User

libname x xlsx 'c:\temp\x.xlsx';

 

proc sql;

select *

 from dictionary.tables

  where libname='X' ;

quit;

Tom
Super User Tom
Super User

Just read the information from the XLSX file yourself.  An XSLX file is just a ZIP file that contains a lot of XML files. The lists of sheet names is in the XML file named xl/workbook.xml.

 

Note that you will need to copy the file out of the workbook and into a new physical file for the XMLV2 engine to read from it.

 

I made a little workbook with three sheets and then moved the order of them around and saved it to disk. Then I ran this code to pull out the list of sheets.

*----------------------------------------------------------------------;
* Copy xl/workbook.xml from XLSX file to physical file ;
*----------------------------------------------------------------------;
filename _wbzip ZIP '3sheets.xlsx' member='xl/workbook.xml';
filename _wb temp ;
data _null_;
rc=fcopy('_wbzip','_wb');
run;

*----------------------------------------------------------------------;
* Generate LIBNAME pointing to copy of xl/workbook.xml from XLSX file ;
*----------------------------------------------------------------------;
filename _wbmap temp;
libname _wb xmlv2 xmlmap=_wbmap automap=reuse;

data sheet_names;
 set _wb.sheet;
run;
proc print width=min;
run;
       sheets_     sheet_                   sheet_
Obs    ORDINAL    ORDINAL    sheet_name    sheetId    sheet_id

 1        1          1       Was_Sheet3       3         rId1
 2        1          2       Was_Sheet1       1         rId2
 3        1          3       Was_Sheet2       2         rId3

The variable SHEET_ORDINAL that the XMLV2 engine generated will have the order of the sheets.

shreyvirmani
Calcite | Level 5

Thanks for this

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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