Hi I have a dataset A with this format
11JAN21:13:15:00.
12JUL02:12:30:02
I want to convert this to separate column of date and time(am and pm).
Date Time
01/11/2021 1:15:00pm
07/12/2002 12:30:02am
Try the DATEPART and TIMEPART functions:
data want;
input DateTimeValue:datetime.;
Date=datepart(DateTimeValue);
Time=timepart(DateTimeValue);
format DateTimeValue datetime. Date mmddyy10. time timeampm.;
datalines;
11JAN21:13:15:00
12JUL02:12:30:02
;
Result:
Obs | DateTimeValue | Date | Time |
---|---|---|---|
1 | 11JAN21:13:15:00 | 01/11/2021 | 1:15:00 PM |
2 | 12JUL02:12:30:02 | 07/12/2002 | 12:30:02 PM |
Use the datepart and timepart functions respectively.
Try the DATEPART and TIMEPART functions:
data want;
input DateTimeValue:datetime.;
Date=datepart(DateTimeValue);
Time=timepart(DateTimeValue);
format DateTimeValue datetime. Date mmddyy10. time timeampm.;
datalines;
11JAN21:13:15:00
12JUL02:12:30:02
;
Result:
Obs | DateTimeValue | Date | Time |
---|---|---|---|
1 | 11JAN21:13:15:00 | 01/11/2021 | 1:15:00 PM |
2 | 12JUL02:12:30:02 | 07/12/2002 | 12:30:02 PM |
Assuming you have a NUMERIC variable that has a count of seconds that has the DATETIME16. format attached to it so that the numbers are displayed as the strings you showed.
Then just use the DATEPART() and TIMEPART() functions to generate new variables that contain days and seconds. You can then attach appropriate formats to those new variables to have the values displayed in the style you showed.
Let's make a sample dataset with a datetime variable.
data have;
datetime=datetime();
format datetime datetime19.;
run;
Now we can make a new dataset that adds the new date and time variables.
data want;
set have;
date=datepart(datetime);
time=timepart(datetime);
format date mmddyy10. time timeampm. ;
run;
Result
Obs datetime date time 1 05JUN2023:09:36:06 06/05/2023 9:36:06 AM
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
For SAS newbies, this video is a great way to get started. James Harroun walks through the process using SAS Studio for SAS OnDemand for Academics, but the same steps apply to any analytics project.
Find more tutorials on the SAS Users YouTube channel.