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

Dear all,

 

I have the excel file looks like this

#NAME?BDATE
77605Q31/08/2011
15455F03/02/2003
28627908/03/2000
31192X17/02/2006
13016511/12/1990
13029822/02/1994
13041203/03/1994
13052M16/01/2001


When I import this excel file into SAS by the following code,

 

proc import datafile = "G:\PATSTAT_control_variable\Control_variable_Datastream.xlsx"
	OUT=Datastream_bdate DBMS=XLSX replace;
	Sheet="bdate";
	RANGE="A1:B12034";
	GETNAMES=YES;
RUN;

 

it is shown as 

 

#NAME?BDATE
77605Q40786
15455F37655
28627936593
31192X38765
13016533218
13029834387
13041234396
13052M36907

However, I expect to import them as the date value or character value?

 

thanks in advance

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@Alexxxxxxx wrote:

Dear all,

 

I have the excel file looks like this

#NAME? BDATE
77605Q 31/08/2011
15455F 03/02/2003
286279 08/03/2000
31192X 17/02/2006
130165 11/12/1990
130298 22/02/1994
130412 03/03/1994
13052M 16/01/2001


When I import this excel file into SAS by the following code,

 

proc import datafile = "G:\PATSTAT_control_variable\Control_variable_Datastream.xlsx"
	OUT=Datastream_bdate DBMS=XLSX replace;
	Sheet="bdate";
	RANGE="A1:B12034";
	GETNAMES=YES;
RUN;

 

it is shown as 

 

#NAME? BDATE
77605Q 40786
15455F 37655
286279 36593
31192X 38765
130165 33218
130298 34387
130412 34396
13052M 36907

However, I expect to import them as the date value or character value?

 

thanks in advance


The only reason BDATE variable in SAS would have values like that would be if that column in Excel had a mix of date values and character strings. In that case SAS would create a character value (you can store numbers as string, but you cannot easily store text as numbers).  When SAS (excel?) does that with dates you get the actual number that Excel stores, but as a string of digits.

 

The right fix is to find the character values in the Excel file and convert them to actual date values.  Then the data will import as a numeric field with a date format attached and SAS will adjust the number to match the numbers SAS uses for those date.

 

If you have to deal with the text you have in BDATE then first convert it to a number and then adjust the number to reflect how SAS stores dates compared in how Excel does. Make sure to attach a date format so the numbers are displayed in human readable form.

data want;
  set have;
  date=input(bdate,32.)+'30DEC1899'd ;
  format date yymmdd10.;
run;

View solution in original post

1 REPLY 1
Tom
Super User Tom
Super User

@Alexxxxxxx wrote:

Dear all,

 

I have the excel file looks like this

#NAME? BDATE
77605Q 31/08/2011
15455F 03/02/2003
286279 08/03/2000
31192X 17/02/2006
130165 11/12/1990
130298 22/02/1994
130412 03/03/1994
13052M 16/01/2001


When I import this excel file into SAS by the following code,

 

proc import datafile = "G:\PATSTAT_control_variable\Control_variable_Datastream.xlsx"
	OUT=Datastream_bdate DBMS=XLSX replace;
	Sheet="bdate";
	RANGE="A1:B12034";
	GETNAMES=YES;
RUN;

 

it is shown as 

 

#NAME? BDATE
77605Q 40786
15455F 37655
286279 36593
31192X 38765
130165 33218
130298 34387
130412 34396
13052M 36907

However, I expect to import them as the date value or character value?

 

thanks in advance


The only reason BDATE variable in SAS would have values like that would be if that column in Excel had a mix of date values and character strings. In that case SAS would create a character value (you can store numbers as string, but you cannot easily store text as numbers).  When SAS (excel?) does that with dates you get the actual number that Excel stores, but as a string of digits.

 

The right fix is to find the character values in the Excel file and convert them to actual date values.  Then the data will import as a numeric field with a date format attached and SAS will adjust the number to match the numbers SAS uses for those date.

 

If you have to deal with the text you have in BDATE then first convert it to a number and then adjust the number to reflect how SAS stores dates compared in how Excel does. Make sure to attach a date format so the numbers are displayed in human readable form.

data want;
  set have;
  date=input(bdate,32.)+'30DEC1899'd ;
  format date yymmdd10.;
run;

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
  • 1 reply
  • 540 views
  • 0 likes
  • 2 in conversation