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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Fugue
Quartz | Level 8

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

5 REPLIES 5
Fugue
Quartz | Level 8

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;

 

 

Hudson33
Calcite | Level 5

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

Thanks again!

Fugue
Quartz | Level 8

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;

PaulaC
Fluorite | Level 6
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.
P_trivedi
Calcite | Level 5

Dear PaulaC,

 

Refer below code for your mentioned query:

 

data dateC;
input chardate :$9.;
datalines;
31dec1999
;
run;

data dateN;
set dateC;
format sasdate date9.;
sasdate=input(chardate,date9.);
run;

 

Placement of format statement does not affect at all.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 103091 views
  • 3 likes
  • 4 in conversation