DATA Step, Macro, Functions and more

import all sheets in excel file

Reply
Frequent Contributor
Posts: 117

import all sheets in excel file

I want to import all the sheets in an excel file with the following conditions:

 

1) import all sheets without having to specify sheet names (there are too many)

2) import row 6 as the heading (column name) and delete rows 1-5 for each sheet. Each sheet has the same column names.

3) Preferably wan to append each sheet to get 1 dataset. Again, each sheet as the same column names starting on row 6.

 

Anyone have any tips to code this import?

Super User
Posts: 21,546

Re: import all sheets in excel file

1. Assign a libname and get all the sheet names from the libname and proc datasets

2. You can import them all using PROC COPY and then use a macro to process the data sets.

3. You'll get mixed types when you import all of the data so you'll need to verify that first. 

4. if they're all the same data layout then add a rename manually to make it easier on yourself. You can easily do this by taking the first row of the worksheet, transpose it in excel (copy/paste special) and then align the letters which is what SAS will call the data. 

Then build a rename statement:

rename 

A = ID

B = Name

C = SSN

Frequent Contributor
Posts: 117

Re: import all sheets in excel file

libname file XLSX 'C:\Users\.xlsx';

proc copy in=file out=WORK;
run;quit;

libname file;

 

 

So when I do this it copies all of the comments in rows 1-5 and the columns names in row 6 as observations. I just dont want to go and manually deleted these rows for each sheet.

Highlighted
Super User
Posts: 21,546

Re: import all sheets in excel file

You can use the _N_ to process each sheet to delete the rows you don't need. 

Ultimately you're going to have to do some processing.

 

The other option is to get the sheet names and then use a macro to import each sheet in turn using DATAROW to start at Row 7 but then you have to rename the variables. 

 

 

Ask a Question
Discussion stats
  • 3 replies
  • 83 views
  • 1 like
  • 2 in conversation