- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Please don't post identical, or essentially identical posts.
Did you look at the respose to your previous post?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks! This worked beautifully. I appreciate the education.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Yes, this works! Thanks.