BookmarkSubscribeRSS Feed
phoenix95
Calcite | Level 5

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?

 

 

 

 

14 REPLIES 14
PGStats
Opal | Level 21

Add the statement

 

format twowk_sdate twowk_edate mmddyy9.;

and you should see the dates in a recognizable format.

PG
phoenix95
Calcite | Level 5

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!

 

 

phoenix95
Calcite | Level 5

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!

 

 

 

 

PGStats
Opal | Level 21

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
phoenix95
Calcite | Level 5

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

 

Again, many thanks!

PGStats
Opal | Level 21

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
phoenix95
Calcite | Level 5

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?

 

 

PGStats
Opal | Level 21

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
phoenix95
Calcite | Level 5

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?

PGStats
Opal | Level 21

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
samnan
Quartz | Level 8

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;

samnan
Quartz | Level 8

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;

phoenix95
Calcite | Level 5

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!

ballardw
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 14 replies
  • 1568 views
  • 0 likes
  • 4 in conversation