BookmarkSubscribeRSS Feed
Melk
Lapis Lazuli | Level 10

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?

3 REPLIES 3
Reeza
Super User

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

Melk
Lapis Lazuli | Level 10

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.

Reeza
Super User

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. 

 

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 5631 views
  • 1 like
  • 2 in conversation