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

Hello,

 

I would like to convert a date from character to date format in SAS Enterprise Guide 7.1. I've used the format function:

    

data data1; 
          date_new=date_old; 
          format date_new date9.; 
run;

however, when I do so, my date is changed from 11-06-2013 to 06-11-2013, which is not the same date.

   11-06-2013 = 11Jun2013 (correct date)

   06-11-2013 = 06Nov2013 (wrong date)

 

I hope you can help me.  

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi @PernilleBaekke ,

 

Dates are stored as numeric values in SAS. So you first need to convert your character value in a numeric one using the input() function and the correct informat for reading the value (ddmmyy10.).

Then you can apply a date format to your new variable (date9. for example).

 

Hope this help!

data have;
	input date_old $ 1-10;
	cards;
11-06-2013
;
run;

data want;
	set have;
	format date_new date9.;
	date_new = input(date_old, ddmmyy10.);
run;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

The format only changes the way a value is displayed, it does not change the contents. So if a date with format date9. displays as 06NOV2013, that is the value stored, period. You might have a misunderstanding of the display format of variable date_old (ddmmyyd10. vs. mmddyyd10.)

ed_sas_member
Meteorite | Level 14

Hi @PernilleBaekke ,

 

Dates are stored as numeric values in SAS. So you first need to convert your character value in a numeric one using the input() function and the correct informat for reading the value (ddmmyy10.).

Then you can apply a date format to your new variable (date9. for example).

 

Hope this help!

data have;
	input date_old $ 1-10;
	cards;
11-06-2013
;
run;

data want;
	set have;
	format date_new date9.;
	date_new = input(date_old, ddmmyy10.);
run;
ballardw
Super User

@PernilleBaekke wrote:

Hello,

 

I would like to convert a date from character to date format in SAS Enterprise Guide 7.1. I've used the format function:

    

data data1; 
          date_new=date_old; 
          format date_new date9.; 
run;

however, when I do so, my date is changed from 11-06-2013 to 06-11-2013, which is not the same date.

   11-06-2013 = 11Jun2013 (correct date)

   06-11-2013 = 06Nov2013 (wrong date)

 

I hope you can help me.  

Thank you!


So what should the "correct" date be for 06-11-2013? I believe you are implying without stating it that you also expect the second to be treated as 11Jun2013.

 

If you read both character values then SAS expects all of the values to be in either month-day-year or day-month-year order. The default depends on your national language settings as to whether month or day is the default for the first.

 

You do not show the code you used to convert a character to date. But with your given example I do not see how you know the second is "incorrect". If there is another variable that indicates some information then you can test that variable(or variables) and conditionally execute different INPUT statements or set a variable to the appropriate informat and use inputn.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 2660 views
  • 0 likes
  • 4 in conversation