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!
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;
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;
Thanks very much! Would the same be the case for changing DATE9. to the numeric SAS date?
Thanks again!
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;
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.
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.
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.