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. 

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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