BookmarkSubscribeRSS Feed
pooja86kaushal
Calcite | Level 5

Hi , Plz help with the below scenario

I wan to know the first Sheet name (order of creation) in an excel sheet. Is there any way.

Somebody suggested using XML (i.e consider excel as xml)

Use the below code but getting error:

Code:

filename src ZIP '‪C:\Users\pooja\Desktop\Try2.xlsx' member='xl/workbook.xml';
filename des temp;

data _null_;
rc=fcopy('src','des');
run;

filename srcmap temp;
libname des xmlv2 xmlmap=srcmap automap=reuse;

data sheet_names;
set des.sheet;
run;

proc print width= min;
run;

 

Error:

libname des xmlv2 xmlmap=srcmap automap=reuse;
ERROR: With the AUTOMAP= option, the specified XML document must exist.
ERROR: Error in the LIBNAME statement.

 

6 REPLIES 6
japelin
Rhodochrosite | Level 12

Do you have '‪C:\Users\pooja\Desktop\Try2.xlsx'?

 

I tried to submit your code, I got success and error;

 

If xlsx file is exists and not double byte character in filename , I got success.

If xlsx file is not exists or exists double byte character(I tried Japanese, maybe other than language is same) in filename, I got same error.

 

can you try this code?

filename foo ZIP 'C:\Users\pooja\Desktop\Try2.xlsx' member='xl/workbook.xml';

data _null_;
infile foo;
input;
put _infile_;
run;

 

If zip access method got success, you get xml contens in log window.

If you got error "Open failure for XXXXX during attempt to create a local filehandle.", you cannot access your xlsx file.

Then please try a new excel file.

 

 

Ksharp
Super User
I also want know how to get it .
Tom
Super User Tom
Super User

Not sure why the FILENAME statement is having trouble.  Why not just use the INFILE statement directly?

filename des temp;
data _null_;
  infile '‪C:\Users\pooja\Desktop\Try2.xlsx' zip member='xl/workbook.xml' recfm=n;
  file des recfm=n;
  input;
  put _infile_;
run;
filename srcmap temp;
libname des xmlv2 xmlmap=srcmap automap=reuse;

data sheet_names;
set des.sheet;
run;

proc print width= min;
run;
Ksharp
Super User
Tom,
whether there is another way to get it or not? i.e. My sas is 9.2 or 9.3
Tom
Super User Tom
Super User

@Ksharp wrote:
Tom,
whether there is another way to get it or not? i.e. My sas is 9.2 or 9.3

If you are using an old version of SAS that does not support the ZIP libname engine then use your local operating system command for extracting files from a ZIP file to get the XML file with the list of sheets.  If your old version of SAS does not support reading XML then just parse the text of the file yourself with a data step.

Ksharp
Super User

Maybe the following code is ugly and not right solution. But could give it a try .

I just compare the variable name in the FIRST sheet. If same ,then take it as the first sheet name.

options validvarname=any validmemname=extend;

proc import datafile='c:\temp\temp.xlsx' out=_temp_ dbms=xlsx replace;
getnames=yes;
run;

libname x excel 'c:\temp\temp.xlsx';
proc copy in=x out=work;
run;

proc sql noprint;
select quote(name) into : list separated by ' '
 from dictionary.columns
  where libname='WORK' and memname='_TEMP_';

select count(*) into : n
 from dictionary.columns
  where libname='WORK' and memname='_TEMP_';

create table want as
select *
 from dictionary.columns
  where libname='WORK' and memname ne '_TEMP_'
   group by memname 
    having &n.=sum(name in (&list.));

quit;

title 'First Sheet Name:';
proc sql;
select distinct memname from want;
quit;

libname x clear;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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