- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Formats are listed here (I suggest bookmarking it, its a great reference) with them grouped by category
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
This is an awesome reference! Thank you for sharing 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
This worked perfectly! Thank you so much!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content