BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Santt0sh
Lapis Lazuli | Level 10

Hi All,

I'm trying to read an excel file into SAS which has headers on two rows, I tried using the solutions provided here but I was not able to get the desired output.

I thought of reading the .xlsx file twice and assigning the variable names to the merged columns in excel but later realized that the excel sheet/dataset keeps growing for the upcoming months or years.  

I also tried to transpose after importing the .xlsx file with GETNAME = NO OPTION.

Please find the excel attached, Kindly Suggest.

 

 

 

 

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Just tell it to skip the first row.

proc import dbms=xls datafile="&path/&fname"
  out=example replace
;
 range='$A2:';
run;

Results:

Screenshot 2022-03-29 115359.jpg

You could clean up the first 3 columns easily using RETAIN.

data step1;
  length program_type product_type $20;
  retain program_type product_type;
  set example;
  if A ne ' ' then do;
     program_type=A;
     product_type=' ';
  end;
  if B ne ' ' then product_type=B;
  rename c=Line_item q=TOTAL;
  drop A B;
run;

Screenshot 2022-03-29 120504.jpg

View solution in original post

7 REPLIES 7
Santt0sh
Lapis Lazuli | Level 10
The transpose results.
Variable Name Variable Type
C 2
E 2
F 2
G 2
GRAND TOTAL 1
H 2
I 2
J 2
K 2
L 2
M 2
N 2
O 2
PROGRAM_TYPE 2
PRODUCT_TYPE 2
R 2
S 2
T 2
U 2
V 2
W 2
X 2
Y 2
_2020 2
_2021 2
Santt0sh
Lapis Lazuli | Level 10
Hi Kurt,

Thank you for your quick response!

I was not able to upload the file to the existing post.
Kindly find the attachment in the below post.
https://communities.sas.com/t5/SAS-Programming/To-import-an-Excel-file-into-SAS-with-column-headers-...


Thank you for your time and guidance!!

Santt0sh
Lapis Lazuli | Level 10

Hi All,

 

Please accept my Sincere Apologies for posting the same question again.

I missed the attachment in my previous post and I'm not sure how to add an attachment to the existing post (https://communities.sas.com/t5/SAS-Programming/Import-an-excel-sheet-which-has-the-column-header-on-....

 

I'm trying to read an excel file into SAS which has headers on two rows, I tried using the solutions provided here but I was not able to get the desired output.

I thought of reading the .xlsx file twice and assigning the variable names to the merged columns in excel but later realized that the excel sheet/dataset keeps growing for the upcoming months or years.

I also tried to transpose after importing the .xlsx file with GETNAME = NO OPTION.

 


The transpose results.
Variable Name Variable Type
C 2
E 2
F 2
G 2
GRAND TOTAL 1
H 2
I 2
J 2
K 2
L 2
M 2
N 2
O 2
PROGRAM_TYPE 2
PRODUCT_TYPE 2
R 2
S 2
T 2
U 2
V 2
W 2
X 2
Y 2
_2020 2
_2021 2

 

Please find the excel attached, Kindly Suggest.

 

Tom
Super User Tom
Super User

Here is how this Forum displays that XLS file.

Screenshot 2022-03-29 114625.jpg

So how many variables do you see there?

What are the names you would want for them?

Tom
Super User Tom
Super User

Just tell it to skip the first row.

proc import dbms=xls datafile="&path/&fname"
  out=example replace
;
 range='$A2:';
run;

Results:

Screenshot 2022-03-29 115359.jpg

You could clean up the first 3 columns easily using RETAIN.

data step1;
  length program_type product_type $20;
  retain program_type product_type;
  set example;
  if A ne ' ' then do;
     program_type=A;
     product_type=' ';
  end;
  if B ne ' ' then product_type=B;
  rename c=Line_item q=TOTAL;
  drop A B;
run;

Screenshot 2022-03-29 120504.jpg

Santt0sh
Lapis Lazuli | Level 10
Hi Kurt,

Thank you for your Post!!
Using the below mentioned code I was able to read the Excel files with headers on Multiple rows, and I was working on renaming the columns as the excel sheet keeps growing and also calculating the sales percentages for all the years.

Regards,

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