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

Hi,

I'm getting frustrated with date and time variables.

I have an access database and I have two variables:  (1) Date (e.g., 2005-03-25) and (2) time in 24 hours format (e.g., 15:12:11, that is HH:MM:SS). In access they are considered character variables.

How can I put both of these variables into ONE variable, like YYYYMMDD HH:MM:SS?

In terms of analysis, I would like to eventually be able to calculate the number of days inbetween the two dates and times.

Thanks for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Do you have any of this in SAS? If so what does proc contents tell you about the variables that contain date and time?

Or are you trying to work in Access through SAS? Or read the data from Access into SAS?

 

When working with dates and times in SAS you have to be aware that there is a difference between the VALUE and the display Format. To manipulate dates, times or datetime valued variables you have to make sure the variables are date or time valued. If you have a Character value like 2005-03-25 then you can create a sas date value as below, and time or datetime

data example;
   characterdate = '2005-03-25';
   SasDate = input(characterdate,yymmdd10.);
   charactertime = '15:21:11';
   SasTime = input(charactertime,time.);
   format SASdate yymmdd10. sastime time8.;
   SASDatetime= dhms(SASDate,0,0,SasTime);
   format SASDatetime  e8601dt.;
   /*Alternate*/
   SASDateTime2 = input(catx(' ',characterdate,charactertime),anydtdtm32.);
   format SASDatetime2 datetime20.;
run;

There is not currently an existing SAS datetime format I know that shows datetime values exactly as you want. PROC Format will let you make one.

 

 

The function INTCK will allow you to calculate intervales between two dates, times or datetimes.

View solution in original post

7 REPLIES 7
sharonlee
Quartz | Level 8

Hi,

I have a character variable YYYY-MM-DD HH:MM:SSS (e.g., 2012-05-12 14:55:11) . The time is in 24 hour format.

How do I convert this into a SAS date and time format? 

I'd like to be able to calculate the difference in time eventually.

Thanks.

ballardw
Super User

Please don't post identical, or essentially identical posts.

 

Did you look at the respose to your previous post?

ballardw
Super User

Do you have any of this in SAS? If so what does proc contents tell you about the variables that contain date and time?

Or are you trying to work in Access through SAS? Or read the data from Access into SAS?

 

When working with dates and times in SAS you have to be aware that there is a difference between the VALUE and the display Format. To manipulate dates, times or datetime valued variables you have to make sure the variables are date or time valued. If you have a Character value like 2005-03-25 then you can create a sas date value as below, and time or datetime

data example;
   characterdate = '2005-03-25';
   SasDate = input(characterdate,yymmdd10.);
   charactertime = '15:21:11';
   SasTime = input(charactertime,time.);
   format SASdate yymmdd10. sastime time8.;
   SASDatetime= dhms(SASDate,0,0,SasTime);
   format SASDatetime  e8601dt.;
   /*Alternate*/
   SASDateTime2 = input(catx(' ',characterdate,charactertime),anydtdtm32.);
   format SASDatetime2 datetime20.;
run;

There is not currently an existing SAS datetime format I know that shows datetime values exactly as you want. PROC Format will let you make one.

 

 

The function INTCK will allow you to calculate intervales between two dates, times or datetimes.

sharonlee
Quartz | Level 8

Thanks! This worked beautifully. I appreciate the education.

PGStats
Opal | Level 21

Use informat anydtdtm. to do the conversion and SAS function intck() to calculate number of days between SAS datetime values:

 

data test;
dt1_str = "2012-05-12 14:55:11";
dt2_str = "2012-05-13 04:55:11";

dt1 = input(dt1_str, anydtdtm.);
dt2 = input(dt2_str, anydtdtm.);

days = intck("DTDAY", dt1, dt2);

format dt1 dt2 datetime19.;
run;

proc print; run;
PG
Kurt_Bremser
Super User

As an informat, e8601dt will also accept strings that don't contain a blank instead of the 'T', so you can use it in @PGStats example in place of anydtdtm.

Being stricter with informats makes it easier to catch errors in the input data; anydtdtm might create a valid (but wrong) datetime value from incorrect input data.

sharonlee
Quartz | Level 8

Yes, this works! Thanks.

 

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
  • 7 replies
  • 9165 views
  • 2 likes
  • 4 in conversation