DATA Step, Macro, Functions and more

I am trying to import data from a xlsx file where first 2 rows are header rows.

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

I am trying to import data from a xlsx file where first 2 rows are header rows.

[ Edited ]

The  file which i am trying to import contains variable names at row 3 and data begins at row 4

following is the code I have done 

 

PROC IMPORT OUT = NEW
DATAFILE= "\\temp.xlsx"
DBMS=XLSX REPLACE ;
NAMEROW=3;
DATAROW=4;
GETNAMES=YES;
RUN;

 

I am getting error that Namerow is not used properly.


Accepted Solutions
Solution
‎04-20-2018 07:05 AM
PROC Star
Posts: 2,318

Re: I am trying to import data from a xlsx file where first 2 rows are header rows.

Posted in reply to sas_learnsups
You need to define a range in your spreadsheet.

View solution in original post


All Replies
PROC Star
Posts: 2,318

Re: I am trying to import data from a xlsx file where first 2 rows are header rows.

Posted in reply to sas_learnsups

It's all in the documentation!

 

See section "Available Statements for Importing and Exporting Excel Files Using DBMS=XLS and DBMS=XLSX"

at http://documentation.sas.com/?docsetId=acpcref&docsetTarget=n0msy4hy1so0ren1acm90iijxn8j.htm&docsetV...

 

NAMEROW is not available for DBMS=XLSX, use RANGE instead.

Occasional Contributor
Posts: 13

Re: I am trying to import data from a xlsx file where first 2 rows are header rows.

[ Edited ]

@ChrisNZ: I did go through the documentation you attached above but somehow range option didn't work for me. 

I saved the copy of file in CSV format and then could import it in SAS dataset. 

 

Thank you very much. Your documentation is of much help to me.

 

 

 

Respected Advisor
Posts: 4,675

Re: I am trying to import data from a xlsx file where first 2 rows are header rows.

Posted in reply to sas_learnsups

@sas_learnsups wrote:

Thanks anyways

 


I believe you've missed that @ChrisNZ has given you the full solution and even has taken the time to look-up and post the link to the relevant documentation for you. You need to use RANGE instead of NAMEROW and things will work. ...and the syntax for RANGE is fully documented in the posted link.

 

If you want us to fix your code and post something working and tested then you need to provide the sample data to do so.

Occasional Contributor
Posts: 13

Re: I am trying to import data from a xlsx file where first 2 rows are header rows.

@Patrick- I did go through the documentation provided by "ChrisNZ".

 

I will keep in mind to attach a screenshot or sample data, next time I post a quesion in SAS communities.

 

Many Thanks,

sas_learsups

Solution
‎04-20-2018 07:05 AM
PROC Star
Posts: 2,318

Re: I am trying to import data from a xlsx file where first 2 rows are header rows.

Posted in reply to sas_learnsups
You need to define a range in your spreadsheet.
Super User
Posts: 13,321

Re: I am trying to import data from a xlsx file where first 2 rows are header rows.

Posted in reply to sas_learnsups

@sas_learnsups wrote:

@ChrisNZ: I did go through the documentation you attached above but somehow range option didn't work for me. 

I saved the copy of file in CSV format and then could import it in SAS dataset. 

 

Thank you very much. Your documentation is of much help to me.

 

 

 


Did't work is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data in the form of a dataset, the actual results and the expected results. Data should be in the form of a data step. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

Super User
Super User
Posts: 9,416

Re: I am trying to import data from a xlsx file where first 2 rows are header rows.

Posted in reply to sas_learnsups

Save the XLSX file as CSV.  Have a datastep:

data _null_;
  file "abc.csv";
  infile "abc.csv";
  input;
  if _infile_ ne "title1" and _infile_ ne "title2" then put _infile_;
run;

Drops the two title rows assuming they have the text title1/2.  You can then write a datastep to read in that csv correctly, per your data specifications documentation.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 206 views
  • 3 likes
  • 5 in conversation