BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sarahsasuser
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
sarahsasuser
Quartz | Level 8

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

6 REPLIES 6
novinosrin
Tourmaline | Level 20

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;

 

sarahsasuser
Quartz | Level 8

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?

novinosrin
Tourmaline | Level 20

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

sarahsasuser
Quartz | Level 8

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).

novinosrin
Tourmaline | Level 20

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

sarahsasuser
Quartz | Level 8

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.  

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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