BookmarkSubscribeRSS Feed
Datino
Obsidian | Level 7

If I have a value like YYYYMMDD stored as string (for example 20170123), how can I convert it to SAS datettime?

 

thanks

4 REPLIES 4
Astounding
PROC Star

If it's a string,  you will need to assign the date value to a new variable since SAS stores dates as numeric:

 

newvar = input(stringvar, yymmdd8.);

 

If you actually have separators in your string, such as yyyy/mm/dd you will need to expand the width:

 

newvar = input(stringvar, yymmdd10.);

 

This gives you a SAS date (not a datetime).  While you can convert this to a datetime value, it's not clear why you would want to do that.  You don't actually have any measurement of hours, minutes, or seconds in your data.

 

Datino
Obsidian | Level 7

Thank you, the reason I want to convert to datetime is because I'm learning how to handle both SAS date and datetime formats 🙂

ballardw
Super User

Don't forget to assign a format to the variable so you can see the date value instead of the internal numeric stored value.

 

If you have a date and time parts the function DHMS will create a datetime variable:

 

DT = dhms(date, hours, minutes, seconds);

If you don't have any time values you can set a default such as

DT = dhms(date,0,0,0); which would be midnight at the start of the day.

DanielSantos
Barite | Level 11

Also usually...

 

SAS Date values are stored as the number of days since 1960/01/01

SAS Time values are stored as the number of  seconds since midnight

SAS Datetime values are stored as the number of seconds since 1960/01/01

 

Knowing that, you can easily switch between Date, Datetime and time values just by doing some maths:

 

DATETIME = DATE*(24*60*60) 

DATE = int(DATETIME/(24*60*60)) 

TIME = mod(DATETIME,24*60*60)

 

Or you could use the dedicated functions:

 

Datepart, that will return the Date value of it's Datetime value argument

Timepart, that will return the Time vale of it's Datetime value argument

DHMS, that will return the Datetime value of it's Date value argument (+ the hour, minute and second specified)

 

More on Date, Datime and Time values here:

 

http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a002200738.htm

 

Hope it helps.

 

Daniel Santos @ www.cgd.pt

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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