DATA Step, Macro, Functions and more

working with date/time variables

Reply
Occasional Contributor
Posts: 7

working with date/time variables

Hello, 

 

I have a question about two date/time variable in my dataset....The first is an interview start date & time and the second is the interview end date & time. When I run a proc contents it says the informat for this variable is ANYDTDTM40. and the format is DATETIME. (the result is this: 08NOV15:15:56:00). I would like to convert this to a MMDDYY10. variable, but cannot figure it out! I have checked out other posts on similar topics but have been unsuccessful.

 

In my datastep, I most recenlty tried created two new variables using the dates from the original variables but it's not working:

twowk_sdate = datepart(v8);

twowk_edate = datepart(v9);

 

Any suggestions?

 

 

 

 

Respected Advisor
Posts: 4,640

Re: working with date/time variables

Add the statement

 

format twowk_sdate twowk_edate mmddyy9.;

and you should see the dates in a recognizable format.

PG
Occasional Contributor
Posts: 7

Re: working with date/time variables

Thank you for your reply! I added mmddyy9. after the twowk_sdate variable name as well (didn't try it without), and it worked!

 

Thanks again!

 

 

Occasional Contributor
Posts: 7

Re: working with date/time variables

Hi PG,

 

I'm running into another error I am hoping you can advise me on. I am trying to take the difference between 2 dates in SAS. I would like to get the number of days between an interview due date and the actual interview date.

 

As part of my data step I am doing the following:

 

intvwdatdiff = intck ('day', twowkintduedate, twowk_edate);

 

And it's coming up with a date like this 12/07/59 (all with the same year '59). Is this also a formatting issue or am I using the wrong function to take the difference between the two dates?

 

Thank you!

 

 

 

 

Respected Advisor
Posts: 4,640

Re: working with date/time variables

The number of days between two dates is not a date, and shouldn't be given a date format. 

 

Try 

 

format intvwdatdiff 5.0;

 

 

PG
Occasional Contributor
Posts: 7

Re: working with date/time variables

That worked! Thank you PG. If you have the time, can you explain what the formatting with the 5.0 did?

 

Again, many thanks!

Respected Advisor
Posts: 4,640

Re: working with date/time variables

Formatting only affect the way a value is represented, it doesn't change the value itself. The 5.0 format requests that the number of days be represented as a number with zero decimals in a field of 5 characters.

BTW, the strange values that you got before was because SAS uses 01JAN1960 as a reference for its internal representation of dates. 

PG
Occasional Contributor
Posts: 7

Re: working with date/time variables

Thank you again, this is great. My calculations seem a bit off though. I used this line in the data step:

 

intvwdatdiff = intck ('day', babydob, twowk_edate); 

 

along with the format intvwdatdiff 5.0;

 

My results though show the following:

 

intvwdatdiff = 7 for a woman with babydob of 2/24/15 and follow-up interview date of 3/6/15. When I look at a 2015 calendar  that's actually a 10 day difference, not a 7 day difference. Some of the other calculations are correct though. For example:

intvwdatdiff = 10 where babydob = 2/15/15 and follow-up interview date of 2/25/15.

 

Any idea why this is happening?

 

 

Respected Advisor
Posts: 4,640

Re: working with date/time variables

No. As far as I know, intnx gives reliable results.

Going to an unambiguous date format such as yymmdd10. might help diagnose the problem.

Try to create a small example where the problem shows up.

PG
Occasional Contributor
Posts: 7

Re: working with date/time variables

Hi PG,

 

Thanks for your response! I think I figured out why my date calculations are off. In February 2015 there were only 28 days (there are 29 days in Feb this year). So the miscalculation or the difference of 3 days from what the actual calculation should have been -- must be b/c February 2015 is missing the 29th, 30th, and 31st. In other words, I wonder if the intck command assumes 31 days in a month?

Respected Advisor
Posts: 4,640

Re: working with date/time variables

To my knowledge nobody ever reported a problem with the intck function. Try the following:

 

data _null_;
do Year = 1995 to 2016;
    date28feb = mdy(2,28,year);
    date01mar = mdy(3,1,year);
    nbDays = intck("DAY", date28feb, date01mar);
    put (_all_) (=);
    end;
format date: yymmdd10.;
run;
PG
Contributor
Posts: 53

Re: working with date/time variables

Hi @phoenix95

 

try this:

 


data have;
DATE = "08NOV15:15:56:00";
run;


data want;
set have;
NewDate = input( scan(DATE,1,':'),date9.);
NewTime = input (substr(DATE ,9,5),time6.);
format NewDate MMDDYY10. NewTime time6.;
run;

Contributor
Posts: 53

Re: working with date/time variables

or you can use this (b data _null_ and Reeza )

 

data have;

original_var = "08NOV15:15:56:00";
dt_var=input(original_var, datetime16.);
date_part=datepart(dt_var);
time_part=timepart(dt_var);
format dt_var datetime16. date_part MMDDYY10. time_part time6.;
run;

proc print data=have;
run;

Occasional Contributor
Posts: 7

Re: working with date/time variables

Hi Samnan,

 

Thank you for your reply! I tried what looked like the simplest solution from the first commenter and it worked. Thank you though for taking the time to reply, I greatly appreciate it!

Super User
Posts: 10,458

Re: working with date/time variables

For most purposes you don't need an additional variable, only a different format.

data _null_;
   x = "08NOV15:15:56:00"dt;
   put  "with long datetime format "  x= datetime. "with short " x= datetime9.;
run;
Ask a Question
Discussion stats
  • 14 replies
  • 347 views
  • 0 likes
  • 4 in conversation