01-27-2016 01:24 PM
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);
01-27-2016 02:37 PM
Thank you for your reply! I added mmddyy9. after the twowk_sdate variable name as well (didn't try it without), and it worked!
01-27-2016 04:07 PM
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?
01-28-2016 11:50 AM
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.
01-28-2016 12:04 PM
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?
01-28-2016 02:46 PM
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.
02-05-2016 11:57 AM
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?
02-05-2016 12:08 PM
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;
01-27-2016 02:01 PM
DATE = "08NOV15:15:56:00";
NewDate = input( scan(DATE,1,':'),date9.);
NewTime = input (substr(DATE ,9,5),time6.);
format NewDate MMDDYY10. NewTime time6.;
01-27-2016 02:11 PM
or you can use this (b data _null_ and Reeza )
original_var = "08NOV15:15:56:00";
format dt_var datetime16. date_part MMDDYY10. time_part time6.;
proc print data=have;
01-27-2016 02:39 PM
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!
01-27-2016 03:45 PM
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;