character date to Date9.

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

character date to Date9.

I suppose there is an easy fix for this, although I am new to SAS so it seems a little tricky at the moment.

Basically I have a date variable that is in character format ( $ 10) and looks like 06/31/2009, as an example. I would like to get that to date9. format if possible.

Thanks for the help!


Accepted Solutions
Solution
‎11-09-2016 03:45 PM
Super Contributor
Posts: 307

Re: character date to Date9.

[ Edited ]

Editor's Note:  Thanks to Fugue for providing an example of how to use the INPUT function to convert a character date into a SAS date and then using the FORMAT statement to apply the desired format.

 

 

Try this:

Data want;
set have;
format datevar date9.;
datevar = input ( chardate, MMDDYY10.);
run;

 

 

View solution in original post


All Replies
Solution
‎11-09-2016 03:45 PM
Super Contributor
Posts: 307

Re: character date to Date9.

[ Edited ]

Editor's Note:  Thanks to Fugue for providing an example of how to use the INPUT function to convert a character date into a SAS date and then using the FORMAT statement to apply the desired format.

 

 

Try this:

Data want;
set have;
format datevar date9.;
datevar = input ( chardate, MMDDYY10.);
run;

 

 

New Contributor
Posts: 2

Re: character date to Date9.

Thanks very much! Would the same be the case for changing DATE9. to the numeric SAS date?

Thanks again!

Super Contributor
Posts: 307

Re: character date to Date9.

SAS date and time values are stored as numbers, but displayed according to the chosen format. Example:

Data want;

set have;

format formatted_date date9. /* displays stored date value with specified format */

     not_formatted_date best. /* number format */;

formatted_date = input ( chardate, MMDDYY10.); /* stores value as a SAS date value (number). Format determines which date format is used to display it. */

not_formatted_date = formatted_date; /* also a numeric value, but format displays it as a number */

run;

Contributor
Posts: 43

Re: character date to Date9.

My current character value is stored as ddmmmyyyy so I used date9. when changing it to the numeric value. The problem is that when I look at my data set the value for that variable is the SAS date. I tried using "format dateqol date9.;" to get it into the correct format, but I am getting the following error message: ERROR 48-59: The format $DATE was not found or could not be loaded.
What am I doing wrong? I put the format statement prior to the input statement. Thanks.
☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 24518 views
  • 1 like
  • 3 in conversation