BookmarkSubscribeRSS Feed
marleeakerson
Calcite | Level 5

Hello, 

I am having some trouble with date formatting. I am inputting a table that has a date variable that looks like this: 

 

marleeakerson_0-1612217843539.png

But when I try to format it using this code:

format cp_createdat MMDDYY10. ;

I get this in the output table: 

marleeakerson_1-1612217903974.png

Does anyone why this is/how to fix it? I am hoping to format this variable to be in the MM/DD/YYYY format. 

 

Thank you!

 

4 REPLIES 4
Reeza
Super User

You have a DATETIME variable, not a DATE variable. You need to use a DATETIME format, or convert it to a DATE variable. 

 

DATEPART will convert a datetime variable to a date variable. 

 

Or you can try adding DT in front of the desired format.

 

format cp_createdat DTMMDDYY10. ;

Or a full conversion:

cp_create_date = datepart(cp_createdat);
format cp_create_date MMDDYY10. ;

Here's a great, but longer and in depth, reference for dates and times in SAS
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...

PaigeMiller
Diamond | Level 26
format cp_createdat dtdate.;

Not the exact formatting you asked for, but it works.

--
Paige Miller
Shmuel
Garnet | Level 18

CP_Created is a datetime variable. Use DATEPART() function to extrat the date only from it:

 

cp_created = datepart(cp_created);
format cp_created mmddyy10. ;

If the input is char type then yo need first to convert it to a numeric sas date variable:

date = input(scan(cp_created,1,':'),date7.);
format date mmddyy10.;

 

ballardw
Super User

The reason you see ********** is that the underlying numeric value is larger than is expected by the MMDDYY format by about a factor of 86,400. Dates are measured by days in SAS and  datetime values like you have are measured in seconds. So one "day" of a date time is 86,400 seconds and way to large for consideration as a date.

 

You need to talk to whoever is creating that file and beat them severely with wet spaghetti noodles for using a 2-digit year.

 

Second, the values you show represent what I call the "lazy Microsoft office programmer" of allowing a "date" to default to a datetime field with 0 hours, 0 minutes and 0 seconds. Useless and ugly if there is not actual time component used.

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!

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
  • 4 replies
  • 1046 views
  • 1 like
  • 5 in conversation