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

Hi everyone,

 

I'm a relatively new SAS user and finding the formatting of dates and times confusing.

 

I am trying to combine DATE and TIME into one DATE_TIME variable. My dates are numeric, and I've formatted them as yymmdd10. I've looked into concatenating, dhms, and a few other random tidbits I've found and just can't seem to get close to what I need. In an attempt to try something else, I also separated out each component into month, day, year, minute, hour, and second variables - in case that helps with another solution. 

 

I need the new variable format to look like yyyy-mm-dd hh:mm. 

 

Can anyone help me with this? I'm happy to provide more information as needed! I'm using SAS 9.4.

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

One way is a custom format such as:

proc format library=work;
picture mydatetime
low-high ='%Y-%0m-%0d %0H:%0M'    (datatype=datetime)
;
run;

data _null_;
  x = '18MAR2008:03:00:00'dt;
  put x= mydatetime.;
run;

Since your example did not include any seconds this does not display any. the zeroes in 0m 0d 0H and 0M are so months, days, hours and minutes are displayed with a leading 0 when less than 10.

 

Do not use double quotes in the picture definition here as that will involve the macro processor and not work as expected.

View solution in original post

7 REPLIES 7
Reeza
Super User

Assuming you already have them as SAS date and times you can use a function, DHMS. 

 

SAS stores dates as the number of days from January 1, 1960, and times as the number of seconds, so they are typically numbers. But that doesn't work for humans so to present it to look like a date, you use a SAS format to control the appearance. You can do this via a FORMAT statement. See the example below. 

 

data demo;
date = today();
time = time();

date_time = dhms(date, 0, 0, time);

format date_time datetime20. date date9. time time.;
run;

proc print data=demo;
run;

@leila4 wrote:

Hi everyone,

 

I'm a relatively new SAS user and finding the formatting of dates and times confusing.

 

I am trying to combine DATE and TIME into one DATE_TIME variable. My dates are numeric, and I've formatted them as yymmdd10. I've looked into concatenating, dhms, and a few other random tidbits I've found and just can't seem to get close to what I need. In an attempt to try something else, I also separated out each component into month, day, year, minute, hour, and second variables - in case that helps with another solution. 

 

I need the new variable format to look like yyyy-mm-dd hh:mm. 

 

Can anyone help me with this? I'm happy to provide more information as needed! I'm using SAS 9.4.

 

Thanks!



 

leila4
Fluorite | Level 6

Thanks so much! This works great, except that I need the format to go from:

 

18MAR2008:03:00:00

 

to

 

2008-03-18 03:00

 

Is there a different format that I can make this happen with?

 

And, thanks for the resource! I'll definitely give it a look.

Reeza
Super User

Formats are listed here (I suggest bookmarking it, its a great reference) with them grouped by category

http://documentation.sas.com/?docsetId=leforinforref&docsetTarget=n0p2fmevfgj470n17h4k9f27qjag.htm&d...

 

I genuinely don't know if that format exists, but you can search the list and see. If it does not, you can roll your own, but I'd avoid that if possible. 

leila4
Fluorite | Level 6

This is an awesome reference! Thank you for sharing 🙂

ballardw
Super User

One way is a custom format such as:

proc format library=work;
picture mydatetime
low-high ='%Y-%0m-%0d %0H:%0M'    (datatype=datetime)
;
run;

data _null_;
  x = '18MAR2008:03:00:00'dt;
  put x= mydatetime.;
run;

Since your example did not include any seconds this does not display any. the zeroes in 0m 0d 0H and 0M are so months, days, hours and minutes are displayed with a leading 0 when less than 10.

 

Do not use double quotes in the picture definition here as that will involve the macro processor and not work as expected.

leila4
Fluorite | Level 6

This worked perfectly! Thank you so much!!

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 connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 46461 views
  • 6 likes
  • 3 in conversation