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 excel files with multiple sheets. From each excel file, if sheet name is as “data” then I would like to get all column headings’ names. How can I do that in SAS. Thank you.
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

XLS files are much harder as there is no XLS libname engine to use.

1248  libname xls xls "c:\downloads\test_class.xls" ;
ERROR: The XLS engine cannot be found.
ERROR: Error in the LIBNAME statement.

If you are running on windows (and you can get it to work) you might be able to use the EXCEL libname engine instead.

 

If you want to only see the names of the variables in the datasets that have the string 'DATA' in their name then just subset the CONTENTS data.

proc print data=contents;
  where memname like '%DATA%' ;
  var memname varnum name;
run;

View solution in original post

6 REPLIES 6
Reeza
Super User

Figure it out for one and then figure out how to scale it.

If you assign a libname to the Excel file and then check the data tab does it have the column headers?

libname myxl 'path to xlsx file';

proc contents data=myxl.data;
run;
Emma2021
Quartz | Level 8
Sorry, can you write the complete code? I have to use sheet name as “data” since there can be multiple sheets. Thank you.
AhmedAl_Attar
Rhodochrosite | Level 12

Hi @Emma2021 

I'll use portion of @Tom's code

libname myfile xlsx 'myfile.xlsx';
proc contents data=myfile._all_ out=contents noprint;
run;
libname myfile clear;

/* Extract the "DATA" sheet column headers */
DATA want(keep=name);
set contents(keep=name memname where=(memname='DATA'));
run;
Tom
Super User Tom
Super User

You can just use the XLSX engine to treat the workbook as a library of datasets.

The NAME of the variables will be derived from the headers in the first row of each sheet.

libname myfile xlsx 'myfile.xlsx';
proc contents data=myfile._all_ out=contents noprint; run;
proc print data=contents;
  by memname;
  id memname;
  var varnum name label ;
run;
Emma2021
Quartz | Level 8
The excel files are xls or xlsx and also each excel file has multiple sheets —I only need column names for sheet names “data”. How can I do that?
Tom
Super User Tom
Super User

XLS files are much harder as there is no XLS libname engine to use.

1248  libname xls xls "c:\downloads\test_class.xls" ;
ERROR: The XLS engine cannot be found.
ERROR: Error in the LIBNAME statement.

If you are running on windows (and you can get it to work) you might be able to use the EXCEL libname engine instead.

 

If you want to only see the names of the variables in the datasets that have the string 'DATA' in their name then just subset the CONTENTS data.

proc print data=contents;
  where memname like '%DATA%' ;
  var memname varnum name;
run;

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
  • 568 views
  • 4 likes
  • 4 in conversation