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

I have a date (has month day year time) which Proc Contents says is datetime16. I want to convert it to a character string of format mm/dd/yyyy, with slashes and no time. 

I'm using SAS 9.4, 64bit, on a Mac through Parallels.

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisHemedinger
Community Manager

Create a new 10-byte char variable, and PUT the date part of the value using the MMDDYY10. format.  The DATEPART function will create the date value that you can format.

 

data want;
  set have; /* data with a datetime var, let's call it dt */
  length chardate $ 10;
  chardate = put(datepart(dt), mmddyy10.);
run;

But... why?  Perhaps just to export to something else for a text-based report?  While the value is in SAS, you can format and report/analyze without having to create a new variable.

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.

View solution in original post

3 REPLIES 3
ChrisHemedinger
Community Manager

Create a new 10-byte char variable, and PUT the date part of the value using the MMDDYY10. format.  The DATEPART function will create the date value that you can format.

 

data want;
  set have; /* data with a datetime var, let's call it dt */
  length chardate $ 10;
  chardate = put(datepart(dt), mmddyy10.);
run;

But... why?  Perhaps just to export to something else for a text-based report?  While the value is in SAS, you can format and report/analyze without having to create a new variable.

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
ballardw
Super User

If you are ever likely to want to sort on the data, or possibly group by month and year, or year and quarter or similar you might consider adding another date variable with the appropriate default format.

 

dateonly = datepart(datetimevariable);

format dateonly mmddyy10. ;

 

Using the dateonly variable for the x-axis of a graph will have values sort in chronologically. If you use a character then you get a sort order like: 01/01/2015, 01/01/2016, 01/02/2015, 01/02/2016 which is not often the desired appearance.

 

 

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
  • 3 replies
  • 6370 views
  • 0 likes
  • 3 in conversation