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
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.
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;
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?
Can you paste a sample what your data looks like and a proc contents output?
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).
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
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: