SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Combining Date and Time into one variable

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Combining Date and Time into one variable

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!


Accepted Solutions
Solution
‎04-30-2018 05:44 PM
Super User
Posts: 13,358

Re: Combining Date and Time into one variable

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


All Replies
Super User
Posts: 23,354

Re: Combining Date and Time into one variable

[ Edited ]

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!



 

New Contributor
Posts: 4

Re: Combining Date and Time into one variable

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.

Super User
Posts: 23,354

Re: Combining Date and Time into one variable

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. 

New Contributor
Posts: 4

Re: Combining Date and Time into one variable

This is an awesome reference! Thank you for sharing :-)

Solution
‎04-30-2018 05:44 PM
Super User
Posts: 13,358

Re: Combining Date and Time into one variable

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.

New Contributor
Posts: 4

Re: Combining Date and Time into one variable

This worked perfectly! Thank you so much!!

Super User
Posts: 23,354

Re: Combining Date and Time into one variable

There's a tutorial on dates and times here that may be helpful as well: https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 360 views
  • 3 likes
  • 3 in conversation