DATA Step, Macro, Functions and more

Datapart and Timepart functions

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 110
Accepted Solution

Datapart and Timepart functions

Hi All,

 

I'm trying to use the datepart and timepart functions to extract the date and times from 1 variable called admitdatetime. For example if the value of admitdatetime is 14MAR06:12:15:00 then date=14MAR06 and time=12:15. Further, I would like to create a variable called day that tells me the day of the week the person arrived. 

 

Data want;
set have ;
informat admitdatetime DATETIME16.;
format admitdatetime DATETIME16.;
date_arr= datepart(admitdatetime);
time_arr=timepart(admitdatetime);
day=weekday(date_arr);
run;

 

However, I get missing values for all of these new variables. The admitdatetime is numeric, informat is DATETIME16. so the datepart/timepart function should work. 

 

Thanks,

Sarah


Accepted Solutions
Solution
2 weeks ago
Frequent Contributor
Posts: 110

Re: Datapart and Timepart functions

Posted in reply to novinosrin

OK, I played around with the excel file and figured out that it will work if I right justify the datetime column in excel. It doesn't appear differently in the imported SAS file (still left justified), but now the datepart and timepart work.  

View solution in original post


All Replies
PROC Star
Posts: 1,296

Re: Datapart and Timepart functions

Posted in reply to sarahsasuser

what's the need for informat statement when reading a datetime variable from a dataset that has datetime values?

Data want;
set have ;
format admitdatetime DATETIME20..;
date_arr= datepart(admitdatetime);
time_arr=timepart(admitdatetime);
day=weekday(date_arr);
run;

 

Frequent Contributor
Posts: 110

Re: Datapart and Timepart functions

Posted in reply to novinosrin

The informat was 16. and I added the datetime informat. I have tried running the code with only the format as you suggested below and it did not work. Neither did changing both informat and formats to the datetime20. Other ideas?

PROC Star
Posts: 1,296

Re: Datapart and Timepart functions

Posted in reply to sarahsasuser

Can you paste a sample what your data looks like and a proc contents output?

Frequent Contributor
Posts: 110

Re: Datapart and Timepart functions

Posted in reply to novinosrin

Sure the contents are attached. The data are protected but the data look like this: 08MAY18:13:37:00

This has been imported from an excel spreadsheet (xls).

PROC Star
Posts: 1,296

Re: Datapart and Timepart functions

Posted in reply to sarahsasuser

strange as my test worked fine

log:

 

354 data test;
355 datetime='08MAY18:13:37:00'dt;
356 date_arr= datepart(datetime);
357 time_arr=timepart(datetime);
358 day=weekday(date_arr);
359 put _all_;
360 format datetime datetime. date_arr date9. time_arr time5. ;
361 run;

datetime=08MAY18:13:37:00 date_arr=08MAY2018 time_arr=13:37 day=3

Solution
2 weeks ago
Frequent Contributor
Posts: 110

Re: Datapart and Timepart functions

Posted in reply to novinosrin

OK, I played around with the excel file and figured out that it will work if I right justify the datetime column in excel. It doesn't appear differently in the imported SAS file (still left justified), but now the datepart and timepart work.  

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 100 views
  • 0 likes
  • 2 in conversation