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

Hi All

 

I have an Excel file that I need to import from another team to SAS. Usually I would save it as a CSV and import using a data step with an infile statement. But for various reasons i don't want to do that for this particular excersice - I need to import the Excel file directly. Please see attached an example of the file structure. The first row is a heading that I want to skip, with the variable names in the second row and the data from there on.

I looked on the forum and for some reason the code that people suggest to do this, gives me an error. When I try and run the code below

proc import datafile= "C:\temp\book3b.xlsx"
	out= Test_Data replace
	DBMS= Excel;
	sheet= "Test Data";
/*	startrow=3;*/
/*	namerow=2;*/
	getnames= yes;
/*	mixed=yes;*/
/*	scantext=yes;*/
/*	usedate=yes;*/
	datarow=3;
 run;

I get "ERROR 180-322: Statement is not valid or it is used out of proper order." on the 'datarow' line.

1) How can I ignore the first row?

2) is there a way i can tell the import procedure I want to exclude certain columns? The 'Comments' column has a high potential to give errors and inconsistancies, and as such i would just like to ignore it.

 

Thnx

 

GD

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Helpful little note in the documentation, which did work as expected.

 

https://support.sas.com/documentation/cdl/en/acpcref/69731/HTML/default/viewer.htm#n0msy4hy1so0ren1a...

 

Specify RANGE="Sheetname$A#:0"; where # is the first data row. Thus, RANGE="sheet1$A3:0"; starts to read the data at row 3. If you use RANGE= for this purpose, do not specify the DATAROW= statement.

 

proc import out=test datafile='/folders/myfolders/input.xlsx' dbms=xlsx replace; getnames=yes;
range="Sheet1$A2:0";
run;

View solution in original post

4 REPLIES 4
Reeza
Super User

Try DBMS=XLSX and DATAROW=3. 

 

I couldn't get it to work with GETNAMES and DATAROW though.

Reeza
Super User

Helpful little note in the documentation, which did work as expected.

 

https://support.sas.com/documentation/cdl/en/acpcref/69731/HTML/default/viewer.htm#n0msy4hy1so0ren1a...

 

Specify RANGE="Sheetname$A#:0"; where # is the first data row. Thus, RANGE="sheet1$A3:0"; starts to read the data at row 3. If you use RANGE= for this purpose, do not specify the DATAROW= statement.

 

proc import out=test datafile='/folders/myfolders/input.xlsx' dbms=xlsx replace; getnames=yes;
range="Sheet1$A2:0";
run;
GenDemo
Quartz | Level 8

Nope, its still not working. It doesn't matter what I do, it keeps on taking the first row (the one I want to ignore) as the variable names. Log window gives no errors or notes. The range statement makes no difference. If I comment out the range stament with the datarow statement in, I can either get the variable names in as the first row, or exclude it.

 

proc import datafile= "C:\temp\book3b.xlsx"

out= Test_Data replace

DBMS= XLSX;

sheet= "Test Data";

getnames= yes;

/* datarow=2;*/

range="Test Data$A2:0";

run;

 

So I can go the log way around and import it like this with the variable names in the first row, and then somehow create a new dataset from this....

 

GD


book3b.PNG
GenDemo
Quartz | Level 8

ah...the sheet statement with the range statement confuses the thing. Silly that it doesn't give you an error or a note on this.

 

proc import datafile= "C:\temp\book3b.xlsx"

out= Test_Data replace

DBMS= XLSX;

/* sheet= "Test Data";*/

getnames= yes;

/* datarow=2;*/

range="Test Data$A2:0";

run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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