DATA Step, Macro, Functions and more

Creating SAS One Date and Time variable (YYYYMMDD HH:MM:SS)

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

Creating SAS One Date and Time variable (YYYYMMDD HH:MM:SS)

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:MMSmiley FrustratedS). In access they are considered character variables.

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

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!


Accepted Solutions
Solution
‎10-14-2016 11:55 AM
Super User
Posts: 11,343

Re: Creating SAS One Date and Time variable (YYYYMMDD HH:MM:SS)

Posted in reply to sharonlee

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


All Replies
Occasional Contributor
Posts: 17

Converting character to date format using YYMMDD HHMMSS in 24 hour time

Posted in reply to sharonlee

Hi,

I have a character variable YYYY-MM-DD HH:MMSmiley FrustratedSS (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.

Super User
Posts: 11,343

Re: Converting character to date format using YYMMDD HHMMSS in 24 hour time

Posted in reply to sharonlee

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

 

Did you look at the respose to your previous post?

Solution
‎10-14-2016 11:55 AM
Super User
Posts: 11,343

Re: Creating SAS One Date and Time variable (YYYYMMDD HH:MM:SS)

Posted in reply to sharonlee

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.

Occasional Contributor
Posts: 17

Re: Creating SAS One Date and Time variable (YYYYMMDD HH:MM:SS)

Thanks! This worked beautifully. I appreciate the education.

Respected Advisor
Posts: 4,932

Re: Creating SAS One Date and Time variable (YYYYMMDD HH:MM:SS)

Posted in reply to sharonlee

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
Super User
Posts: 7,854

Re: Creating SAS One Date and Time variable (YYYYMMDD HH:MM:SS)

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 17

Re: Creating SAS One Date and Time variable (YYYYMMDD HH:MM:SS)

Yes, this works! Thanks.

 

☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 466 views
  • 2 likes
  • 4 in conversation