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

Hi Everyone,

I need to import a sheet of an Excel file (xlsx) into SAS using data step (infile rather than import).

Also, 1 column is date in the format Feb-25-2019 and I don't know how to read it correctly.

Can you please help?

Many thanks,

HHC

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@hhchenfx wrote:

Hi Everyone,

I need to import a sheet of an Excel file (xlsx) into SAS using data step (infile rather than import).

Also, 1 column is date in the format Feb-25-2019 and I don't know how to read it correctly.

Can you please help?

Many thanks,

HHC


Nope.  An XLSX file is a complex combination of XML files compressed into a ZIP file with and extension of .XLSX instead of .ZIP.  Reading that with an INPUT statement is if not impossible, then definitely not something you want to even attempt.

 

If all of the CELLS in the COLUMN in the worksheet have DATE values then it should not matter how EXCEL is displaying the date, it will come over to SAS as a NUMERIC variable with DATE values.  In which case there is nothing you will need to do.

 

But if the column has a mix of numeric cells (dates are numeric in EXCEL just like in SAS) and character cells then the it will come over to SAS as a CHARACTER variable.  In that case you can then run a separate data step to make a new NUMERIC variable with date values.

 

Let's assume you have created a SAS dataset named HAVE by reading the XSLX sheet (either with PROC IMPORT or by using a libref with the XLSX engine) and it has a variable named XDATE with that strange string of characters in it. Then you could try doing something like this to make a new dataset named WANT and a new variable named DATE.

data want;
 set have;
 date = input(xdate,anydtdte11.);
 format date date9.;
run;

Let's make an example dataset and see if it works:

data have;
  xdate='Feb-25-2019';
run;

Result

Tom_0-1708750905135.png

 

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

@hhchenfx wrote:

Hi Everyone,

I need to import a sheet of an Excel file (xlsx) into SAS using data step (infile rather than import).

Also, 1 column is date in the format Feb-25-2019 and I don't know how to read it correctly.

Can you please help?

Many thanks,

HHC


Nope.  An XLSX file is a complex combination of XML files compressed into a ZIP file with and extension of .XLSX instead of .ZIP.  Reading that with an INPUT statement is if not impossible, then definitely not something you want to even attempt.

 

If all of the CELLS in the COLUMN in the worksheet have DATE values then it should not matter how EXCEL is displaying the date, it will come over to SAS as a NUMERIC variable with DATE values.  In which case there is nothing you will need to do.

 

But if the column has a mix of numeric cells (dates are numeric in EXCEL just like in SAS) and character cells then the it will come over to SAS as a CHARACTER variable.  In that case you can then run a separate data step to make a new NUMERIC variable with date values.

 

Let's assume you have created a SAS dataset named HAVE by reading the XSLX sheet (either with PROC IMPORT or by using a libref with the XLSX engine) and it has a variable named XDATE with that strange string of characters in it. Then you could try doing something like this to make a new dataset named WANT and a new variable named DATE.

data want;
 set have;
 date = input(xdate,anydtdte11.);
 format date date9.;
run;

Let's make an example dataset and see if it works:

data have;
  xdate='Feb-25-2019';
run;

Result

Tom_0-1708750905135.png

 

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
  • 2 replies
  • 361 views
  • 0 likes
  • 3 in conversation