BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
alepage
Barite | Level 11

Hello,

 

I am trying to proc import an Excel file (xlsx), but I am not able to properly import the variable names.

 

%let sname1='hyfi prm cnts'n;
PROC IMPORT
DATAFILE='/.../Inforce_N_Hyfi_count02JUL21.xlsx'
OUT=test
DBMS=xlsx
REPLACE;
SHEET="&sname1.";
range="&sname1.$A5:S500";
GETNAMES=YES;
RUN;

 

The range option is not working

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Excel is not going like that letter N in the RANGE setting.  You don't have to set the end of the range, just the upper left corner.

PROC IMPORT DATAFILE='/.../Inforce_N_Hyfi_count02JUL21.xlsx'
  DBMS=xlsx
  OUT=test REPLACE
;
  RANGE="'hyfi prm cnts'$A5:";
  GETNAMES=YES;
RUN;

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

What happens when you omit the SHEET= statement?

 

My personal solution would be to save the data to a delimited (comma, tab, whatever) text file and read that with a data step.

alepage
Barite | Level 11

%let sname1='hyfi prm cnts'n;
PROC IMPORT
DATAFILE='/.../Inforce_N_Hyfi_count02JUL21.xlsx'
OUT=test
DBMS=xlsx
REPLACE;
/* SHEET="&sname1.";*/
range="&sname1.$A5:S500";
GETNAMES=YES;
RUN;

 

Couldn't find sheet in spreadsheet
Requested Input File Is Invalid
ERROR: Import unsuccessful. See SAS Log for details.

 

Tom
Super User Tom
Super User

Excel is not going like that letter N in the RANGE setting.  You don't have to set the end of the range, just the upper left corner.

PROC IMPORT DATAFILE='/.../Inforce_N_Hyfi_count02JUL21.xlsx'
  DBMS=xlsx
  OUT=test REPLACE
;
  RANGE="'hyfi prm cnts'$A5:";
  GETNAMES=YES;
RUN;
Kurt_Bremser
Super User

On second thought, I see that you try to use a SAS name literal as a sheet name (something you would do if you used LIBNAME XLSX and the sheet as a pseudo-dataset).

But so, the RANGE statement will resolve to

range="'hyfi prm cnts'n$A5:S500";

and I have doubts that this will work unless the sheet is actually named 'hyfi prm cnts'n (including the quotes and the trailing n).

jebjur
SAS Employee

Syntax such as the following should work:

 

%let sname=sheet1$;

 

proc import datafile = C:\myfiles\Excel Files\class.xlsx"
out = test

dbms = xlsx replace;
range = "&sname.A5:E30";
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 5 replies
  • 1727 views
  • 3 likes
  • 4 in conversation